in reply to Re: cannot get Benchmark to work
in thread cannot get Benchmark to work
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]; } 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% --
#!/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% --
|
|---|