Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Edit: thanks remiah for showing me the error of my ways. While updating the benchmark test functions, I also changed the benchmark itself from 'timethese' to 'cmpthese' which displays the results in order of slowest to fastest. Using map is in fact slower than using foreach.

I realized that output should be spewing all over my console with the test right after I hit send (Doh!)

Here's my revised benchmark, with STDOUT redirected to null. Map is still 'not' the clear leader. verbose, verbose2, verbose4 and verbose4 ('for' instead of 'foreach') are all within statistical noise of each other (and can change significantly with each run), so none of them are any better than the other.

Printing to null might also have an affect on the results, but it's clear that map is 'not' faster. Maybe because for/foreach must also be ready for a 'next' or 'last' or even a 'return' within the loop? I may have to re-think when I use foreach vs map.

#!/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 verbose4 { my $hash = shift; print "$_: $hash->{$_}\n" for sort keys %$hash; } sub idiom { my $hash = shift; print map "$_: $hash->{$_}\n", sort keys %$hash; } cmpthese(-10, { 'Verbose' => sub{ local *STDOUT; open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!"; verbose(\%h) }, 'Verbose2' => sub{ local *STDOUT; open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!"; verbose2(\%h) }, 'Verbose3' => sub{ local *STDOUT; open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!"; verbose3(\%h) }, 'Verbose4' => sub{ local *STDOUT; open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!"; verbose4(\%h) }, 'Idiom' => sub{ local *STDOUT; open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!"; idiom(\%h) }, });
Results: Rate Idiom Verbose Verbose4 Verbose3 Verbose2 Idiom 14273/s -- -9% -11% -12% -12% Verbose 15703/s 10% -- -2% -3% -3% Verbose4 16029/s 12% 2% -- -1% -1% Verbose3 16129/s 13% 3% 1% -- -0% Verbose2 16151/s 13% 3% 1% 0% --

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

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 exploiting the Monastery: (4)
    As of 2014-08-30 13:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (293 votes), past polls