Below is the performance test I built. Tests 1 + 2 are on Inputstreams, 3 + 4 on Readers, and 2 & 4 are Buffered.
When I run this on my machine (AMD Athlon 64 dual core 1 GHz, 1 GB RAM), what I see is that the readers are fairly consistantly faster than the streams, but more interesting (to me at least) there seems to be no difference between the times with the raw stream / reader and the buffered version, with the unbuffered version sometimes outperforming the buffered version!
Here's the question:
1. Are there any circumstances where BufferedInputStream or BufferedReader will outperform the unbuffered version? If not, it seems to me like the only point of these classes is to add a ReadLine method... Should they be deprecated?
public static void IOPerformanceTest() throws IOException {
File f = new File("iotest.txt");
FileWriter fw = new FileWriter(f);
int size = 10 * 1000 * 1000;
Random r = new Random(System.currentTimeMillis());
for (int i = 0 ; i < size; i++) {
char c = (char) ('A' + r.nextInt(26));
fw.write(c);
}
fw.close();
System.out.println("exists: " + f.exists());
System.out.println("size: " + f.length());
Reader rd;
char[] chars = new char[size];
long time, time2;
rd = new FileReader(f);
rd = new BufferedReader(rd);
time = System.nanoTime();
System.out.println(rd.read(chars));
time2 = System.nanoTime();
System.out.println("time4: " + (time2 - time));
f.delete();
rd = new FileReader(f);
time = System.nanoTime();
System.out.println(rd.read(chars));
time2 = System.nanoTime();
System.out.println("time3: " + (time2 - time));
InputStream in = new FileInputStream(f);
byte[] buf = new byte[size];
time = System.nanoTime();
System.out.println(in.read(buf));
time2 = System.nanoTime();
System.out.println("time1: " + (time2 - time));
in = new FileInputStream(f);
in = new BufferedInputStream(in);
time = System.nanoTime();
System.out.println(in.read(buf));
time2 = System.nanoTime();
System.out.println("time2: " + (time2 - time));
}