Check out the Benchmark module (perldoc Benchmark). Here's what it says:
#!/usr/bin/perl use strict; use Benchmark; sub line_by_line { open (IN, "/usr/share/dict/words"); while (<IN>){ if ($_ =~ /hello world/){ # do what ever } } close IN; } sub block { open (IN, "/usr/share/dict/words"); my @lines = <IN>; close (IN); foreach (@lines){ if ($_ =~ /hello world/){ # do what ever } } } timethese(100,{ 'line' => \&line_by_line, 'block' => \&block });
Which Yields
Benchmark: timing 100 iterations of block, line... block: 32 wallclock secs (32.52 usr + 0.36 sys = 32.88 CPU) @ 3 +.04/s (n=100) line: 19 wallclock secs (17.92 usr + 0.16 sys = 18.08 CPU) @ 5 +.53/s (n=100)

/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Updated:Fixed regexp to be the same in both subs. Used question's code verbatim. Thanks lemming.


In reply to Re: which is quicker? by maverick
in thread Quickest way of reading in large files? by Mac

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.