File::ReadBackwards is significantly faster in this scenario.
use strict; use warnings; use Benchmark; use Tie::File; use File::ReadBackwards; my $file = "bench.txt"; #60 MB timethese ( 5, { 'bw' => sub { my $bw = File::ReadBackwards->new( $file ) or die "can't r +ead '$_[0]' $!" ; my @lines; $lines[0] = $bw->readline; if ( $lines[0] =~ /;738409000;/ ) { for (1..9) { $lines[$_] = $bw->readline; } } @lines = reverse @lines; return \@lines; }, 'tied' => sub { my @array; my @lines; tie @array, 'Tie::File', $file or die $!; if ( $array[-1] =~ /;738409000;/ ) { for (1..10) { $lines[$_-1] = $array[(11-$_)*-1] . "\n"; } } return \@lines; } } );
yields
Benchmark: timing 5 iterations of bw, tied... bw: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count) tied: 69 wallclock secs (50.44 usr + 14.67 sys = 65.11 CPU) @ 0 +.08/s (n=5)
Edit:

Above benchmark was done with a 60 Megabyte text file.
Using a 2 Kilobyte file and 1500 iterations:
Benchmark: timing 1500 iterations of bw, tied... bw: 1 wallclock secs ( 0.36 usr + 0.30 sys = 0.66 CPU) @ 22 +83.11/s (n=1500) tied: 5 wallclock secs ( 2.05 usr + 3.31 sys = 5.36 CPU) @ 27 +9.90/s (n=1500)
That can easily be explained. Tie::File always reads the whole file, File::ReadBackwards only reads the neccessary data.


holli, /regexed monk/

In reply to Re^2: pattern matching only last line of a file and then copy 10 lines above till end by holli
in thread pattern matching only last line of a file and then copy 10 lines above till end by ultibuzz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.