in reply to pure perl tail

Well, to respond to Merlyn, I started thinking about how to do it after reading a reply he wrote on clpm that File::Backwards and Tie::File are good, but not quite the fastest. So I thought, hmmm, how would I do this? The code is the most straight forward approach I could come up with. Just start at the EOF and count back byte by byte, counting newlines, until I satified my $numlines test. I tested it against the following File::Backwards script on a 100 meg file, and my script was faster by a small amount, plus I don't need to use a module. I used the system time command to time them.
#!/usr/bin/perl use File::ReadBackwards; #usage tailfilebackwards filename numlines my $filename = shift or die "Usage: $0 file numlines\n"; my $numlines = shift; $bw = File::ReadBackwards->new($filename) or die "can't read $filename $!" ; $count=0; while(defined($line = $bw->readline)){ push @lines,$line ; $count++; if ($count == $numlines){last} } @lines= reverse @lines; print "@lines\n"; exit;
My sample times follow:
For 3 tries -> time tailfilebackwards ARCHIVE 10
real    0m0.078s
user    0m0.040s
sys     0m0.010s

real    0m0.060s
user    0m0.040s
sys     0m0.010s

real    0m0.077s
user    0m0.040s
sys     0m0.000s
###########################################################
For 3 tries -> time tailz ARCHIVE 10

real    0m0.051s
user    0m0.010s
sys     0m0.000s

real    0m0.056s
user    0m0.010s
sys     0m0.010s

real    0m0.056s
user    0m0.010s
sys     0m0.020s
##########################################################
Now I admit that my method is non-portable, so the modular methods are better in that respect, but time-wise they are not. If anyone cares to comment on how I might make my method better, I would appreciate that.

Replies are listed 'Best First'.
Re^2: pure perl tail
by suyashjain (Initiate) on Apr 26, 2013 at 16:58 UTC
    Wow..This is g8...i always believe that i must have full control on what i am coding...predefined modules are good but not ever...they definitely contains "extra" code which is not required by us and in large projects it "MATTERS".