I've been messing around with the Benchmark module and experimenting what the quickest way is to approach file-reading. I'm currently benchmarking the following code working on a 2.5 MB wordlist:
use Benchmark qw/countit cmpthese/; sub run($) { countit(1, @_) } cmpthese { read_proc => run q{ open(WORDS,"words.txt") or die("Wordlist unavaliable.\n"); my @words = <WORDS>; close(WORDS); foreach $word (@words){ chomp $word; if ($word =~ m/[aeiouyAEIOUY]{4,}/){ push(@hitwords,$word); $hitcounter++; } $counter++; } }, for_proc => run q{ open(WORDS,"words.txt") or die("Wordlist unavaliable.\n"); foreach $word (<WORDS>){ chomp $word; if ($word =~ m/[aeiouyAEIOUY]{4,}/){ push(@hitwords,$word); $hitcounter++; } $counter++; } close(WORDS); }, while_proc => run q{ open(WORDS,"words.txt") or die("Wordlist unavaliable.\n"); while($word = <WORDS>){ chomp $word; if ($word =~ m/[aeiouyAEIOUY]{4,}/){ push(@hitwords,$word); $hitcounter++; } $counter++; } close(WORDS); } };
I would expect while_proc to be the fastest by far and the entire chunck of code should take only a few minutes to run. However, when I run it, it takes hours and then gives me this as a result:
s/iter for_proc read_proc while_proc for_proc 5983 -- -6% -100% read_proc 5645 6% -- -100% while_proc 2.42 246713% 232775% --
Now I know that can't be right. Each block of code runs fine by itself, and while_proc IS the fastest version of the code. But the Benchmark results don't verify that at all. What am I doing wrong? I know I need to run more iterations for a reliable benchmark, but I can't really do that if one takes half the day.

In reply to Benchmarking File Retrevial by jpfarmer

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.