in reply to Speed reading (files)
I got this results:use Benchmark qw(:all); use File::Slurp; $file_slurp = sub { $file = read_file('arq.pdf', binmode => ':raw') }; $read = sub { open $file, '<', 'arq.pdf' || die $!; read $file, +$data, -s 'arq.pdf'; close $file; }; timethese(1000, { file_slurp => $file_slurp, read => $read, }); cmpthese(1000, { file_slurp => $file_slurp, read => $read, });
I see that file_slurp in this case is the best option. I've used a 80k file with 1000 lines and 79 columns.Benchmark: timing 1000 iterations of file_slurp, read... file_slurp: 1 wallclock secs ( 0.43 usr + 0.31 sys = 0.74 CPU) @ 13 +51.35/s (n=1000) read: 6 wallclock secs ( 4.88 usr + 0.44 sys = 5.32 CPU) @ 18 +7.97/s (n=1000) Rate read file_slurp read 192/s -- -86% file_slurp 1333/s 595% --
Using a 2.5Mb pdf file the results were different:
I this case File::Slurp was slower than read(). I see that File::Slurp has a binopen mode. After changing to binmode:file_slurp: 44 wallclock secs (15.99 usr + 26.40 sys = 42.39 CPU) @ 23 +.59/s (n=1000) read: 23 wallclock secs ( 9.12 usr + 13.67 sys = 22.79 CPU) @ 43 +.88/s (n=1000) Rate file_slurp read file_slurp 23.6/s -- -46% read 43.6/s 85% --
I think in bigger files read() has better performance than File::Slurp... I don't know why but I hope it helps :-)file_slurp: 46 wallclock secs (16.01 usr + 26.25 sys = 42.26 CPU) @ 23 +.66/s (n=1000) read: 25 wallclock secs ( 9.00 usr + 13.91 sys = 22.91 CPU) @ 43 +.65/s (n=1000) Rate file_slurp read file_slurp 23.7/s -- -46% read 43.5/s 84% --
|
|---|