Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I ran some benchmarks and I the real answer is that while <..> has been finely tuned. On a large file it is almost as fast as foreach over a list.
/dev/null
               Rate        for     whyfor      while while_list   for_list
for        374878/s         --       -13%       -15%       -55%       -62%
whyfor     433016/s        16%         --        -2%       -48%       -56%
while      442469/s        18%         2%         --       -47%       -55%
while_list 833025/s       122%        92%        88%         --       -16%
for_list   991880/s       165%       129%       124%        19%         --

/usr/share/dict/words
             Rate        for while_list     whyfor      while   for_list
for        4.80/s         --       -35%       -37%       -38%       -43%
while_list 7.40/s        54%         --        -2%        -5%       -13%
whyfor     7.57/s        58%         2%         --        -2%       -11%
while      7.75/s        62%         5%         2%         --        -9%
for_list   8.48/s        77%        15%        12%         9%         --

/etc/passwd
              Rate        for     whyfor      while while_list   for_list
for        14440/s         --       -13%       -16%       -24%       -49%
whyfor     16599/s        15%         --        -4%       -12%       -42%
while      17224/s        19%         4%         --        -9%       -39%
while_list 18915/s        31%        14%        10%         --       -33%
for_list   28442/s        97%        71%        65%        50%         --
#!/usr/bin/perl use strict; use Benchmark qw( cmpthese ); foreach my $file qw ( /dev/null /usr/share/dict/words /etc/passwd ) { open my $IN1, '<', $file or die "could not open $file"; my @list = <$IN1>; seek( $IN1, 0, 0 ); print "$file\n"; cmpthese( -5, { for_list => sub { my %counts = (); ++$counts{$_} for @list; die unless keys %counts == @list; }, while_list => sub { my $x = 0; my %counts = (); ++$counts{$_} while defined( $_ = $list[ $x++ ] ); die unless keys %counts == @list; }, for => sub { seek( $IN1, 0, 0 ); my %counts = (); ++$counts{$_} for <$IN1>; die unless keys %counts == @list; }, while => sub { seek( $IN1, 0, 0 ); my %counts = (); ++$counts{$_} while <$IN1>; die unless keys %counts == @list; }, whyfor => sub { seek( $IN1, 0, 0 ); my %counts = (); for ( ; defined( $_ = <$IN1> ) ; ) { ++$counts{$_}; } die unless keys %counts == @list; }, } ); }
-- gam3
A picture is worth a thousand words, but takes 200K.

In reply to Re: Why is "for" much slower than "while"? by gam3
in thread Why is "for" much slower than "while"? by di

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (11)
    As of 2014-07-30 20:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (240 votes), past polls