Search code examples
javaprofilingbenchmarkingniomemory-mapped-files

Testing different file read methods in Java


I'm doing a little bit of algorithm profiling, and I've decided to test three file read methods, and then benchmark them (compare their average execution times). First I generate a larde text file (several hundred MBs), and then run ten tests for each method - buffered reader, normal IO read, and memory mapped reading:

public static void bufferedRead(String filename) {
    BufferedReader br = null;

    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader(filename));
        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

public static void NIORead(String filename) throws IOException {
    RandomAccessFile aFile = new RandomAccessFile(filename, "r");
    FileChannel inChannel = aFile.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while (inChannel.read(buffer) > 0) {
        buffer.flip();
        for (int i = 0; i < buffer.limit(); i++) {
            System.out.print((char) buffer.get());
        }
        buffer.clear(); 
    }
    inChannel.close();
    aFile.close();

}

public static void memoryMapRead(String filename) throws IOException {
    RandomAccessFile aFile = new RandomAccessFile(filename, "r");
    FileChannel inChannel = aFile.getChannel();
    MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY,
            0, inChannel.size());
    buffer.load();
    for (int i = 0; i < buffer.limit(); i++) {
        System.out.print((char) buffer.get());
    }
    buffer.clear(); 
    inChannel.close();
    aFile.close();
}

However, the entire process (3x10 measurements) takes a really long time, like 9 hours or so. True, I don't have a SSD disk, but still, it seems really long to me, even for a 400 MB text file. My question is: are those time results plausible? If not, is there anything that looks incorrect in those implementations?


Solution

  • Removing System.out.println(...) may improve the performance of your benchmark, but make sure to do something with the read String, so the loops do not get optimized out.