There are many ways to read the last line of a file in perl, but as usual, not all are equal.
#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use Tie::File; use File::ReadBackwards; for our $file ( qw[ data/500k.dat data/1000k.dat data/2MB.dat ]) { print "\nComparing $file"; cmpthese( -3, { 'Tie::File' => q[ my( @lines, $last ); tie @lines, 'Tie::File', $file or die $!; $last = $lines[ -1 ]; untie @lines; # print "TF:$last"; ], 'File::ReadBackwards' => q[ my $last; tie *FILE, 'File::ReadBackwards', $file or die $!; $last = <FILE>; untie *FILE; # print "RB:$last"; ], rawio => q[ my( $last, $buffer ); open FILE, '< :raw', $file or die $!; sysseek FILE, -1000, 2; sysread FILE, $buffer, 1000; $last = substr $buffer, 1+rindex( $buffer, "\n", length($b +uffer)-2 ); close FILE; # print "raw:$last"; ], readfwd => q[ my $last; open FILE, '<', $file or die $!; $last = <FILE> until eof FILE; close FILE; # print "RF:$last"; ], }); } __END__ P:\test>354830 Comparing data/500k.dat Rate Tie::File readfwd File::ReadBackwards + rawio Tie::File 5.12/s -- -94% -99% + -100% readfwd 90.0/s 1657% -- -88% + -99% File::ReadBackwards 775/s 15042% 762% -- + -95% rawio 14765/s 288372% 16314% 1805% + -- Comparing data/1000k.dat Rate Tie::File readfwd File::ReadBackwards + rawio Tie::File 2.51/s -- -95% -100% + -100% readfwd 45.9/s 1730% -- -95% + -100% File::ReadBackwards 854/s 33931% 1759% -- + -94% rawio 15214/s 605971% 33011% 1681% + -- Comparing data/2MB.dat Rate Tie::File readfwd File::ReadBackwards + rawio Tie::File 1.25/s -- -95% -100% + -100% readfwd 22.9/s 1736% -- -98% + -100% File::ReadBackwards 1040/s 83151% 4434% -- + -93% rawio 15087/s 1207959% 65694% 1351% + --
Note: The figures are for 3 files (25 character lines), on one OS (Win32XP) and perl 5.8.3. YMMV.
In reply to Reading from the end of a file. by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |