Why is ReversedLinesFileReader so slow?
<p>I have a file that is 21.6GB and I want to read it from the end to the start rather than from the beginning to the end as you would usually do.</p>

<p>If I read each line of the file from the start to the end using the following code, then it takes 1 minute, 12 seconds.</p>

<pre><code>val startTime = System.currentTimeMillis()
File("very-large-file.xml").forEachLine {
    val i = 0
}
val diff = System.currentTimeMillis() - startTime
println(diff.timeFormat())
</code></pre>

<p>Now, I have read that to read in a file in reverse then I should use <code>ReversedLinesFileReader</code> from Apache Commons.  I have created the following extension function to do just this:</p>

<pre><code>fun File.forEachLineFromTheEndOfFile(action: (line: String) -&gt; Unit) {
    val reader = ReversedLinesFileReader(this, Charset.defaultCharset())
    var line = reader.readLine()
    while (line != null) {
        action.invoke(line)
        line = reader.readLine()
    }

    reader.close()
}
</code></pre>

<p>and then call it in the following way, which is the same as the previous way only with a call to <code>forEachLineFromTheEndOfFile</code> function:</p>

<pre><code>val startTime = System.currentTimeMillis()
File("very-large-file.xml").forEachLineFromTheEndOfFile {
    val i = 0
}
val diff = System.currentTimeMillis() - startTime
println(diff.timeFormat())
</code></pre>

<p>This took <strong>17 minutes and 50 seconds</strong> to run!</p>

<ul>
<li>Am I using <code>ReversedLinesFileReader</code> in the correct way?</li>
<li>I am running Linux Mint with an Ext4 file system on an SSD.  Could this have anything to do with it?</li>
<li>Is it just the case that files should not be read from the end to the start?</li>
</ul>