BufferReader.skip () performance

只谈情不闲聊 提交于 2019-12-11 07:55:33

问题


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

}

And

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

}

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?

Thanks In Advance.


回答1:


The skip for bytes can skip that many bytes without reading them.

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

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)



来源:https://stackoverflow.com/questions/9517367/bufferreader-skip-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!