I would not do that. It hides what you want to benchmark not behind one, but two subroutine calls. Which are expensive in Perl. Much better is it to use strings, as shown in the benchmark below. It shows that the foreach statement modifier is faster than the map BLOCK variant (which isn't at all surprising as the modifier doesn't need to create a scope - on addition to that, the map BLOCK variant makes an additional copy of the list), but while the double sub shows a 63% speed difference, the string version shows a 92% difference.
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); our @foo = 1 .. 10; sub a { my @list = @{shift()}; $_ += 1 foreach @list; return \@list; } sub b { my @list = @{shift()}; return [map {$_ += 1} @list]; } cmpthese(500_000, { a => sub {a(\@foo)}, b => sub {b(\@foo)}, c => 'my @list = @foo; $_ += 1 foreach @list; \@list', d => 'my @list = @foo; [map {$_ += 1} @list]', }); __END__ Rate b d a c b 68587/s -- -9% -39% -52% d 74963/s 9% -- -33% -48% a 111607/s 63% 49% -- -22% c 143678/s 109% 92% 29% --
Note that the fastest solution is map EXPR in void context (well, at least on my computer), as shown by the benchmark below:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); our @foo = 1 .. 10; sub a { my @list = @{shift()}; $_ += 1 foreach @list; return \@list; } sub b { my @list = @{shift()}; return [map {$_ += 1} @list]; } sub c { my @list = @{shift()}; map {$_ += 1} @list; return \@list; } sub d { my @list = @{shift()}; map $_ += 1, @list; return \@list; } cmpthese(250_000, { a1 => sub {a(\@foo)}, b1 => sub {b(\@foo)}, c1 => sub {c(\@foo)}, d1 => sub {d(\@foo)}, a2 => 'my @list = @foo; $_ += 1 foreach @list; \@list', b2 => 'my @list = @foo; [map {$_ += 1} @list]', c2 => 'my @list = @foo; map {$_ += 1} @list; \@list', d2 => 'my @list = @foo; map $_ += 1, @list; \@list', }); __END__ Rate b1 b2 c1 c2 a1 d1 a2 d2 b1 68493/s -- -9% -11% -24% -38% -44% -51% -57% b2 75529/s 10% -- -2% -16% -32% -38% -46% -53% c1 76687/s 12% 2% -- -14% -31% -37% -45% -52% c2 89606/s 31% 19% 17% -- -19% -27% -36% -44% a1 110619/s 62% 46% 44% 23% -- -9% -21% -31% d1 121951/s 78% 61% 59% 36% 10% -- -13% -24% a2 140449/s 105% 86% 83% 57% 27% 15% -- -12% d2 160256/s 134% 112% 109% 79% 45% 31% 14% --

In reply to Re^2: cannot get Benchmark to work by Anonymous Monk
in thread cannot get Benchmark to work by Anonymous Monk

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.