Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am with Limbic System on the NIO recommendation. I've added a NIO method to Daphna's test code and bench marked it against his two methods:</p> <pre><code>public static void timeNioReader () throws IOException { long bef = System.currentTimeMillis(); File file = new File("/Users/stu/test.txt"); FileChannel fc = (new FileInputStream(file)).getChannel(); MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, 0, file.length()); boolean emptyLine = true; int counter = 0; while (buf.hasRemaining()) { byte element = buf.get(); if (element == '\r' || element == '\n') { if (!emptyLine) { counter += 1; emptyLine = true; } } else emptyLine = false; } long after = System.currentTimeMillis() - bef; System.out.println("timeNioReader Time: " + after + " Result: " + counter); } </code></pre> <p>Here are the warmed up results for a 89MB file:</p> <pre><code>timeBufferedReader Time: 947 Result: 747656 timeFileReader Time: 670 Result: 747656 timeNioReader Time: 251 Result: 747656 </code></pre> <p>NIO is 2.5x faster than FileReader and 4x fastser than the BufferedReader!</p> <p>With a 6.4MB file the results are even better, although the warm up time is much longer.</p> <pre><code>//jvm start, warming up timeBufferedReader Time: 121 Result: 53404 timeFileReader Time: 65 Result: 53404 timeNioReader Time: 40 Result: 53404 //still warming up timeBufferedReader Time: 107 Result: 53404 timeFileReader Time: 60 Result: 53404 timeNioReader Time: 20 Result: 53404 //ripping along timeBufferedReader Time: 79 Result: 53404 timeFileReader Time: 56 Result: 53404 timeNioReader Time: 16 Result: 53404 </code></pre> <p>Make of it what you will. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload