Sach
Sach

Reputation: 659

BufferReader.skip () performance

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.

Upvotes: 2

Views: 2487

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

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)

Upvotes: 1

Related Questions