Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Edit: Just so others don't stop here, this benchmark doesn't actually test the functions it claims to. Read the following messages for a more accurate benchmark of map vs foreach. Hint: foreach wins in the end

Hmmm, well now I'm a little surprised. I was going to argue that if you're going to benchmark then you must compare apples to apples. In your example, 'verbose' uses an extra variable 'my $key' which the mapping version does not. I thought that might be a factor, so I made a new function 'verbose2' which uses $_ like the mapping function, expecting that it might be on par (or at least faster than 'verbose').

But using $_ in place of $key was actually slower ???? So I tried one more time with 'verbose3' to re-write the function exactly the same as the mapping version, only using foreach instead. In my mind, verbose2 and verbose3 are exactly the same code and Perl should have interpreted them to be the same at run time, but again verbose3 was slower yet.

Can a Perl innards expert explain why verbose2 is slower than verbose? Or why verbose3 is slower than verbose2?

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); my %h; @h{'A'..'Z','a'..'z'} = 1..52; sub verbose { my $hash = shift; foreach my $key (sort keys %$hash) { print "$key: $hash->{$key}\n"; } } sub verbose2 { my $hash = shift; foreach (sort keys %$hash) { print "$_: $hash->{$_}\n"; } } sub verbose3 { my $hash = shift; print "$_: $hash->{$_}\n" foreach sort keys %$hash; } sub idiom { my $hash = shift; print map "$_: $hash->{$_}\n", sort keys %$hash; } timethese(1000000, { 'Verbose' => 'verbose(\%h)', 'Verbose2' => 'verbose2(\%h)', 'Verbose3' => 'verbose3(\%h)', 'Idiom' => 'idiom(\%h)', }); timethese(10000000, { 'Verbose' => 'verbose(\%h)', 'Verbose2' => 'verbose2(\%h)', 'Verbose3' => 'verbose3(\%h)', 'Idiom' => 'idiom(\%h)', });
Results: Benchmark: timing 1000000 iterations of Idiom, Verbose, Verbose2, Verb +ose3... Idiom: 1 wallclock secs ( 0.85 usr + 0.00 sys = 0.85 CPU) @ 11 +76470.59/s (n=1000000) Verbose: 1 wallclock secs ( 1.26 usr + 0.00 sys = 1.26 CPU) @ 79 +3650.79/s (n=1000000) Verbose2: 0 wallclock secs ( 1.34 usr + 0.00 sys = 1.34 CPU) @ 74 +6268.66/s (n=1000000) Verbose3: 2 wallclock secs ( 1.39 usr + 0.00 sys = 1.39 CPU) @ 71 +9424.46/s (n=1000000) Benchmark: timing 10000000 iterations of Idiom, Verbose, Verbose2, Ver +bose3... Idiom: 8 wallclock secs ( 8.57 usr + 0.00 sys = 8.57 CPU) @ 11 +66861.14/s (n=10000000) Verbose: 12 wallclock secs (12.92 usr + 0.00 sys = 12.92 CPU) @ 77 +3993.81/s (n=10000000) Verbose2: 13 wallclock secs (13.06 usr + 0.00 sys = 13.06 CPU) @ 76 +5696.78/s (n=10000000) Verbose3: 15 wallclock secs (13.90 usr + 0.01 sys = 13.91 CPU) @ 71 +8907.26/s (n=10000000)

In reply to Re^3: Efficiency of map vs. more verbose basic/fundamental code by ruzam
in thread Efficiency of map vs. more verbose basic/fundamental code by marquezc329

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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (8)
    As of 2021-03-06 18:31 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (118 votes). Check out past polls.