本文介绍了BufferReader.skip()性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到下面两个程序之间的性能差异很大。

I can see vast difference in performance between below two programs.

import java.io.*;
import java.util.Date;

class SkipProg2  {

  public static void main (String args[]) {

       System.out.println (" File Reading "+ args.length);
       System.out.println (" 1st Arg "+ args[0]);
       System.out.println (" 2nd Arg "+ args[1]);
        try {
            FileInputStream fis = new FileInputStream(args[0]);

            System.err.println("Time before skip : " + new Date());

            Long off = Long.parseLong(args[1]);

            fis.skip (off);
            System.err.println("Time After skip : " + new Date());


            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr );

          } // end try
          catch (IOException e) {
            System.err.println("Error: " + e);
         }
    } // end main

}

然后

import java.io.*;
import java.util.Date;

class SkipProg  {

  public static void main (String args[]) {

       System.out.println (" File Reading "+ args.length);
       System.out.println (" 1st Arg "+ args[0]);
       System.out.println (" 2nd Arg "+ args[1]);
        try {
            FileInputStream fis = new FileInputStream(args[0]);

            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr );

            System.err.println("Time before skip : " + new Date());
            Long off = Long.parseLong(args[1]);

            br.skip (off);
            System.err.println("Time After skip : " + new Date());
          } // end try
          catch (IOException e) {
            System.err.println("Error: " + e);
         }
    } // end main

}

一个用法FileInputStream.skip()和另一个BufferReader.skip()。但是,如果偏移值较大,则存在巨大差异(例如,对于2 GB而言,Ex。8 Secs有所不同),在多线程应用程序中,相同代码的差异很大(对于2 Gb偏移,则大约有15-20分钟的延迟)。我不能用FileInputStream.skip()替换BufferReader.skip(),因为一个以字节为单位偏移,而另一个以chars偏移。对于unicode文件,它是不可替代的。

One usage FileInputStream.skip () and another BufferReader.skip (). But, if offset value is bigger there vast different (For Ex. 8 Secs different for 2 GB)and in multithreaded application, the difference for the same code is huge (For 2 Gb offset there is around 15-20 mins delay). I cant replace BufferReader.skip () with FileInputStream.skip (), As one takes offset in terms of bytes and another in terms of chars. For unicode file, it is irreplaceable.

第一个问题,我的假设是否正确?有什么建议?

First question, whether my assumption is correct? what are suggestions?

预先感谢。

推荐答案

跳过

chars的跳过必须读取所有chars / bytes直到该点为止,以找到第N个字符。

The skip for chars has to read all the chars/bytes up to that point to find where the Nth character is.

我感到惊讶的是,阅读2 GB的文本需要15到20分钟的时间。我本来希望接近20秒。您拥有哪种硬件?

I am surprised it takes 15-20 mins to read 2 GB of text. I would have expected closer to 20 seconds. What sort of hardware do you have?

如果要在文本文件中进行随机访问,则需要维护行号到字节位置的索引(那样时间会保持一致)

If you want random access in a text file, you need to maintain an index of line number to byte location (that way the time taken will bet the same)

这篇关于BufferReader.skip()性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:39