Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Having been caught once again by 'each' not resetting, I decided to do some benchmarks to compare the performance of a while loop with 'each' and a foreach loop with 'keys'.

The results surprised me, so I'm seeking enlightenment - can anybody explain this one?

Here is the code I tried... (obviously I could have used 'values' in the second case, but this code was constructed just for benchmarking)
#! /usr/bin/perl -w use Benchmark qw(cmpthese); use strict; use integer; my %hash; for (my $i = 0; $i < 1000; $i++) { $hash{$i} = $i; } sub e { my $total = 0; keys %hash; while (my($k, $v) = each %hash) { $total += $v; last if $total > 500; } } sub k { my $total = 0; foreach my $k (keys %hash) { my $v = $hash{$k}; $total += $v; last if $total > 500; } } cmpthese(100000, { 'each' => \&e, 'keys' => \&k });

And here are the results...

Benchmark: timing 100000 iterations of each, keys...
      each:  1 wallclock secs ( 0.61 usr +  0.01 sys =  0.62 CPU) @ 161290.32/s (n=100000)
      keys: 93 wallclock secs (90.03 usr +  0.77 sys = 90.80 CPU) @ 1101.32/s (n=100000)
         Rate   keys   each
keys   1101/s     --   -99%
each 161290/s 14545%     --

...but... (and this is the weird bit) if I comment out the line which breaks out of the loop ('last if...') then the results are completely different... (I dropped the number of iterations - hope this hasn't changed too much - I got bored waiting for it)

Benchmark: timing 1000 iterations of each, keys...
      each:  2 wallclock secs ( 2.77 usr +  0.00 sys =  2.77 CPU) @ 361.01/s (n=1000)
      keys:  3 wallclock secs ( 2.36 usr +  0.00 sys =  2.36 CPU) @ 423.73/s (n=1000)
      Rate each keys
each 361/s   -- -15%
keys 424/s  17%   --

So it seems that 'each' is much quicker, but only if you plan on leaving the loop early - which is when the not resetting gotcha kicks in.

Now I understand that 'keys' would have to generate a list which might be expensive I guess, but I'm still a bit puzzled by the results. Perhaps there is something else going on.

Have fun,

rdw


In reply to each or keys? by rdw

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, 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
  • 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.
  • 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 examining the Monastery: (4)
    As of 2021-04-14 19:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?