Welcome to the Monastery | |
PerlMonks |
Re^3: Efficiency of map vs. more verbose basic/fundamental codeby ruzam (Curate) |
on Oct 04, 2012 at 22:57 UTC ( [id://997341]=note: 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?
In Section
Seekers of Perl Wisdom
|
|