I did this test:
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 got this results:
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% --
I see that file_slurp in this case is the best option. I've used a 80k file with 1000 lines and 79 columns.

Using a 2.5Mb pdf file the results were different:

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 this case File::Slurp was slower than read(). I see that File::Slurp has a binopen mode. After changing to binmode:
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% --
I think in bigger files read() has better performance than File::Slurp... I don't know why but I hope it helps :-)


Igor S. Lopes - izut
surrender to perl. your code, your rules.

In reply to Re: Speed reading (files) by izut
in thread Speed reading (files) by kwaping

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.