in reply to Re: pattern matching only last line of a file and then copy 10 lines above till end
in thread pattern matching only last line of a file and then copy 10 lines above till end
yieldsuse 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; } } );
Edit: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)
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)
|
|---|