Perl's I/O is buffered, so it does one I/O for every disk block regardless of which method you use.
#!/usr/bin/perl if ($ARGV[0] eq 'line') { print "Line-at-a-time\n"; while (<STDIN>) { print if /perl/; } } else { print "All at once\n"; my @arr = (<STDIN>); foreach (@arr) { print if /perl/; } }
On my system, the block size is 4096 bytes. On an 8K file with 128 lines, we see:
$ strace -e read /tmp/t29 line </tmp/t29.8192 >/dev/null
...
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "", 4096)                       = 0

$ strace -e read /tmp/t29 slurp </tmp/t29.8192 >/dev/null
...
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "", 4096)                       = 0
Still, the diamond operator takes some time to operate, so slurping is still probably faster, but not because of I/O.

In reply to Re: Re: Re: Opening file and checking for data by sgifford
in thread Opening file and checking for data by Anonymous Monk

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.