From Raw I/O to Smokin’ I/O

So far we have looked only at general points about I/O and logging. Now we look at an example of tuning I/O performance. The example consists of reading lines from a large file. This section was inspired from an article from Sun Engineering,[54] though I go somewhat further along the tuning cycle.

The initial attempt at file I/O might be to use the FileInputStream to read through a file. Note that DataInputStream has a readLine() method (now deprecated because it is byte-based rather than char-based, but ignore that for the moment), so you wrap the FileInputStream with the DataInputStream, and run. The code looks like:

DataInputStream in = new DataInputStream(new FileInputStream(file));
while ( (line = in.readLine( )) != null)
{
  doSomethingWith(line);
}
in.close( );

For these timing tests, I use two different files, a 1.8-MB file with about 20,000 lines (long lines), and a one-third of a megabyte file with about 34,000 lines (short lines). I will test using several VMs to show the variations across VMs and the challenges in improving performance across different runtime environments. To make comparisons simpler, I report the times as normalized to 100% for the JDK 1.2 VM with JIT. The long-line case and the short-line case will be normalized separately. Tests are averages across at least three test runs. For the baseline test, I have the following chart (see Tables Table 8-1 and Table 8-2 for full results). Note that the HotSpot results are those for ...

Get Java Performance Tuning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.