note
andreychek
<ul>
<i> I wonder if there is any performance/compilation difference between using any of the below.</i><br><br>
</ul>
Interesting question, lets find out. I used [cpan://Benchmark] to figure this out. I ran them through at 50,000 iterations, and I changed all the "print" statements to "warn" statements, so I could pipe STDERR to /dev/null, and still see the results of the benchmark on STDOUT. Here's the code:
<code>
#!/usr/bin/perl -w
use strict;
use Benchmark;
use vars qw/ @a @b /;
@a = ('a'..'z');
timethese(50000, {
'1' => '@b = map uc,@a; warn @b,"\n"',
'2' => '@b = map uc $_,@a; warn @b,"\n"',
'3' => '@b = map uc(),@a; warn @b,"\n"',
'4' => '@b = map uc($_),@a; warn @b,"\n"',
'5' => '@b = map {uc} @a; warn @b,"\n"',
'6' => '@b = map {uc $_} @a; warn @b,"\n"',
'7' => '@b = map {uc()} @a; warn @b,"\n"',
'8' => '@b = map {uc($_)} @a; warn @b,"\n"',
});
</code>
And here are the results:
<code>
Benchmark: timing 50000 iterations of 1, 2, 3, 4, 5, 6, 7, 8...
1: 4 wallclock secs ( 4.55 usr + 0.03 sys = 4.58 CPU) @ 10917.03/s (n=50000)
2: 5 wallclock secs ( 4.51 usr + 0.03 sys = 4.54 CPU) @ 11013.22/s (n=50000)
3: 5 wallclock secs ( 4.53 usr + 0.03 sys = 4.56 CPU) @ 10964.91/s (n=50000)
4: 4 wallclock secs ( 4.54 usr + 0.03 sys = 4.57 CPU) @ 10940.92/s (n=50000)
5: 5 wallclock secs ( 4.52 usr + 0.06 sys = 4.58 CPU) @ 10917.03/s (n=50000)
6: 5 wallclock secs ( 4.52 usr + 0.00 sys = 4.52 CPU) @ 11061.95/s (n=50000)
7: 4 wallclock secs ( 4.34 usr + 0.01 sys = 4.35 CPU) @ 11494.25/s (n=50000)
8: 5 wallclock secs ( 4.31 usr + 0.04 sys = 4.35 CPU) @ 11494.25/s (n=50000)
</code>
While these results show #7 and #8 as being the fastest, I would consider this inconclusive. The fact is, it's so close that every time I run this test, I keep getting wide ranging answers, even with 50,000 iterations.<br><br>
Either way, it was still fun :-)<br>
-Eric<br><br>
<b>Updated:</b> Fixed a booboo pointed out by [chipmunk]. The benchmark actually runs correctly now :-)
120503
120503