On Mon, Apr 21, 2008 at 11:18 AM, Gunnar Hjalmarsson <[EMAIL PROTECTED]> wrote: > Mr. Shawn H. Corey wrote: > > > The fastest way to do this is to read every line into Perl and disregard > everything not relevant. > > > > Don't think so. > > I did a benchmark on a text file with 100,000 lines, where I'm actually > only interested in the 5 last lines. Except for Tie::File, which proved to > be awfully slow, reading and testing every line against a regex took > hundreds of times longer time compared to seek() or File::ReadBackwards. > Please see below. snip
It is even worse than the benchmarks would lead you to believe. Unless you setup your file system and kernel (in)correctly every read from the file after the first benefits from the cache, so the cost of reading the whole file is not accurately reflected in your benchmark (which is why I didn't bother with one). Also, they have different orders of growth. Reading the whole file will get slower as the file size gets bigger, but the method used by File::ReadBackwards will slow down a little and then stay (relatively*) constant. * The cost to walk the inode tree is O(log n), so it grows, but very slowly -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/