Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Try it this way:
#!/usr/bin/perl -w use Benchmark; use strict; use integer; my %top_hash; for (my $i = 0; $i < 1000; $i++) { $top_hash{$i} = $i; } sub e { my $total = 0; my %hash = %top_hash; while (my($k, $v) = each %hash) { $total += $v; last if $total > 500; } } sub k { my $total = 0; my %hash = %top_hash; foreach my $k (keys %hash) { my $v = $hash{$k}; $total += $v; last if $total > 500; } } timethese(500, { 'each' => \&e, 'keys' => \&k });
Why copy the hash? My guess is that there's an internal optimization going on the first time each is called. If you copy the old hash in every time, that optimization won't take place on the hash you're passing in to both subs.

Of course, you might print out the first ten elements of each and see if they're coming out in the same order. If the first two values you get out of each are both above 250, you'll jump out right there. If it takes a dozen from keys to get over 500, you have to go through the loop six more times. (I don't think that's likely, but it's good to check these kinds of assumptions before benchmarking).

My results for the revised code (500 iterations because copying a 1000-element hash is pretty time consuming):

Benchmark: timing 500 iterations of each, keys... each: 4 wallclock secs ( 3.77 usr + 0.01 sys = 3.78 CPU) keys: 5 wallclock secs ( 4.85 usr + 0.00 sys = 4.85 CPU)

In reply to Re: each or keys? by chromatic
in thread 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 pondering the Monastery: (4)
    As of 2021-04-14 20:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?