Unfortunately, piping STDERR to /dev/null hid the key mistake in this benchmark. You have declared @a and @b as lexical variables, and passed the code snippets to Benchmark as strings. The eval of the code snippets occurs within Benchmark, where the lexical @a and @b are not in scope. Thus, all of the code snippets are iterating over an empty array! The difference in execution time that you saw is just noise.

To avoid this problem, you should either declare @a and @b as global variables or pass the code snippets as anonymous subroutine references.

Here's an improved benchmark:

#!/usr/bin/perl -w use 5.006; use strict; use Benchmark qw/ cmpthese /; use vars qw/ @a @b /; @a = ('a'..'z'); cmpthese(-3, { '1' => '@b = map uc,@a', '2' => '@b = map uc $_,@a', '3' => '@b = map uc(),@a', '4' => '@b = map uc($_),@a', '5' => '@b = map {uc} @a', '6' => '@b = map {uc $_} @a', '7' => '@b = map {uc()} @a', '8' => '@b = map {uc($_)} @a', }); __END__ Benchmark: running 1, 2, 3, 4, 5, 6, 7, 8, each for at least 3 CPU sec +onds... 1: 4 wallclock secs ( 3.17 usr + 0.01 sys = 3.18 CPU) @ 97 +06.92/s (n=30868) 2: 4 wallclock secs ( 3.18 usr + 0.02 sys = 3.20 CPU) @ 97 +10.31/s (n=31073) 3: 4 wallclock secs ( 3.13 usr + 0.02 sys = 3.15 CPU) @ 97 +73.02/s (n=30785) 4: 4 wallclock secs ( 3.13 usr + 0.01 sys = 3.14 CPU) @ 98 +98.09/s (n=31080) 5: 3 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 96 +11.67/s (n=30469) 6: 3 wallclock secs ( 3.18 usr + 0.01 sys = 3.19 CPU) @ 96 +50.47/s (n=30785) 7: 4 wallclock secs ( 3.04 usr + 0.02 sys = 3.06 CPU) @ 95 +82.68/s (n=29323) 8: 4 wallclock secs ( 3.32 usr + 0.01 sys = 3.33 CPU) @ 95 +33.33/s (n=31746) Rate 8 7 5 6 1 2 3 4 8 9533/s -- -1% -1% -1% -2% -2% -2% -4% 7 9583/s 1% -- -0% -1% -1% -1% -2% -3% 5 9612/s 1% 0% -- -0% -1% -1% -2% -3% 6 9650/s 1% 1% 0% -- -1% -1% -1% -3% 1 9707/s 2% 1% 1% 1% -- -0% -1% -2% 2 9710/s 2% 1% 1% 1% 0% -- -1% -2% 3 9773/s 3% 2% 2% 1% 1% 1% -- -1% 4 9898/s 4% 3% 3% 3% 2% 2% 1% --
The difference between the solutions is still insignificant. With repeated benchmarks, the order of the solutions will change, and the percentage differences will remain small.

In reply to Re: Re: Tribute to TMTOWTDI by chipmunk
in thread Tribute to TMTOWTDI by s0ttle

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.