Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^3: Efficiency of map vs. more verbose basic/fundamental code

by ruzam (Curate)
on Oct 04, 2012 at 22:57 UTC ( #997341=note: print w/replies, xml ) Need Help??

in reply to Re^2: Efficiency of map vs. more verbose basic/fundamental code
in thread Efficiency of map vs. more verbose basic/fundamental code

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)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997341]
[perldigious]: karlgoethebier: Well it is a pretty old and complicated (for me) bit of code I wrote (poorly by my current standards), so I'm expecting everything to break when I add the scoping and find out what else is undesireably scope changed. :-)
[perldigious]: Ah, thanks choroba, that sort of thing was precisely what I was wondering when I asked.
[perldigious]: I didn't want to tie up memory unecessarily basically, I wanted to "delete" it specifically to free it up, and wasn't sure I was even accomplishing that.
[stevieb]: perldigious You should start by writing some unit tests. That'll ensure current functionality doesn't break with changes.
[choroba]: unit tests++
[choroba]: The only problem you can't solve by adding more tests is the problem of having too many tests.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-07-21 19:57 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (335 votes). Check out past polls.