As always with perl, TIMOWTDI, but the important thing is knowing when to use which. Below are the results from using four of the recommended methods above to read the first and last lines of 3 files. 100kb, 1Mb, and 10Mb. The benchmark isn't very scientific and doesn't take into account such things as caching etc, but the results speak for themselves I think.
Directory of c:\test
03/05/06 02:45p 10,400,001 10000k.txt
03/05/06 02:44p 1,040,000 1000k.txt
03/05/06 02:42p 104,001 100k.txt
3 File(s) 11,544,002 bytes
95,741,952 bytes free
c:\test>255837 100k.txt
Read to array
Read forward
Read backward
Tie::File
1 trial of read-to-array (150.000ms total)
1 trial of read-forward (91ms total)
1 trial of File::ReadBackwards (10ms total)
1 trial of Tie::File (580ms total)
c:\test>255837 1000k.txt
Read to array
Read forward
Read backward
Tie::File
1 trial of read-to-array (1.071s total)
1 trial of read-forward (942ms total)
1 trial of File::ReadBackwards (10ms total)
1 trial of Tie::File (6.359s total)
c:\test>255837 10000k.txt
Read to array
Read forward
Read backward
Tie::File
1 trial of read-to-array (10.475s total)
1 trial of read-forward (10.165s total)
1 trial of File::ReadBackwards (10ms total)
1 trial of Tie::File (66.065s total)
The important thing to note is that broquaints recommendation for File::ReadBackwards takes the same amount of time regardless of the filesize, where as all the other solutions take linearly more time (is that O(n)?) as the filesize increases. Benchmarking maybe eshewed, but it has it's uses.
Benchmark
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
|