Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the fastest version I have found so far, about 6 times faster than readLines. On a 150MB log file this takes 0.35 seconds, versus 2.40 seconds when using readLines(). Just for fun, linux' wc -l command takes 0.15 seconds.</p> <pre><code>public static int countLines(String filename) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(filename)); try { byte[] c = new byte[1024]; int count = 0; int readChars = 0; boolean empty = true; while ((readChars = is.read(c)) != -1) { empty = false; for (int i = 0; i &lt; readChars; ++i) { if (c[i] == '\n') { ++count; } } } return (count == 0 &amp;&amp; !empty) ? 1 : count; } finally { is.close(); } } </code></pre> <p>EDIT, 9 1/2 years later: I have practically no java experience, but anyways I have tried to benchmark this code against the <code>LineNumberReader</code> solution below since it bothered me that nobody did it. It seems that especially for large files my solution is faster. Although it seems to take a few runs until the optimizer does a decent job. I've played a bit with the code, and have produced a new version that is consistently fastest:</p> <pre><code>public static int countLinesNew(String filename) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(filename)); try { byte[] c = new byte[1024]; int readChars = is.read(c); if (readChars == -1) { // bail out if nothing to read return 0; } // make it easy for the optimizer to tune this loop int count = 0; while (readChars == 1024) { for (int i=0; i&lt;1024;) { if (c[i++] == '\n') { ++count; } } readChars = is.read(c); } // count remaining characters while (readChars != -1) { System.out.println(readChars); for (int i=0; i&lt;readChars; ++i) { if (c[i] == '\n') { ++count; } } readChars = is.read(c); } return count == 0 ? 1 : count; } finally { is.close(); } } </code></pre> <p>Benchmark resuls for a 1.3GB text file, y axis in seconds. I've performed 100 runs with the same file, and measured each run with <code>System.nanoTime()</code>. You can see that <code>countLines</code> has a few outliers, and <code>countLinesNew</code> has none and seems to be also slightly faster. <code>LineNumberReader</code> is clearly slower.</p> <p><a href="https://i.stack.imgur.com/fjQQB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fjQQB.png" alt="Benchmark Plot"></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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