in reply to Efficiency of map vs. more verbose basic/fundamental code
Even so, I would like to understand in what ways the map version of the code above would be less efficient than the more verbose foreach loop
There are only two ways it could be, it uses more memory, or it takes longer, you can Benchmark to find out
But I suspect there isn't much difference in either today, since that statement was published in 1998
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Efficiency of map vs. more verbose basic/fundamental code
by marquezc329 (Scribe) on Oct 04, 2012 at 22:13 UTC | |
For the sake of simplicity I used the code from the passage broken into subroutines to be called by Benchmark:
RESULTS: Benchmark: timing 100000 iterations of Idiom, Verbose... Idiom: 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1250000.00/s (n=100000) (warning: too few iterations for a reliable count) Verbose: 0 wallclock secs ( 0.12 usr + 0.00 sys = 0.12 CPU) @ 833333.33/s (n=100000) (warning: too few iterations for a reliable count) Benchmark: timing 1000000 iterations of Idiom, Verbose... Idiom: 1 wallclock secs ( 0.76 usr + 0.00 sys = 0.76 CPU) @ 1315789.47/s (n=1000000) Verbose: 1 wallclock secs ( 1.12 usr + 0.00 sys = 1.12 CPU) @ 892857.14/s (n=1000000) Benchmark: timing 10000000 iterations of Idiom, Verbose... Idiom: 8 wallclock secs ( 7.52 usr + 0.00 sys = 7.52 CPU) @ 1329787.23/s (n=10000000) Verbose: 12 wallclock secs (11.76 usr + 0.00 sys = 11.76 CPU) @ 850340.14/s (n=10000000) So at least for this particular piece of code map proves to be faster. Thanks again for helping me answer my own question. As always, I'm still a beginner so if this is misguided/messy code or i tested this wrong please let me know. | [reply] [d/l] |
by ruzam (Curate) on Oct 04, 2012 at 22:57 UTC | |
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?
| [reply] [d/l] [select] |
by remiah (Hermit) on Oct 04, 2012 at 22:52 UTC | |
I tried your benchmark and I also see map is faster. When I changed subs to return concatenated results, for loop becomes faster. I wonder why? update: I remember previous thread. "for loop" consumes lots of memory compared to while loop. | [reply] [d/l] |
by remiah (Hermit) on Oct 04, 2012 at 23:18 UTC | |
hello, again Personally, I like map. regards. | [reply] [d/l] |
by ruzam (Curate) on Oct 04, 2012 at 23:45 UTC | |
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 Printing to null might also have an affect on the results, but it's clear that map is 'not' faster.
| [reply] [d/l] [select] |
by remiah (Hermit) on Oct 05, 2012 at 01:38 UTC | |
by ruzam (Curate) on Oct 05, 2012 at 14:28 UTC | |