in reply to Re: Re: When would you choose foreach instead of map?
in thread When would you choose foreach instead of map?
use strict; use warnings; use Benchmark qw(:all) ; my $to = 10_000; cmpthese(-5, { 'Map100k' => sub { my $sum = 0; map { $sum += $_ } 1 .. 100_000; }, 'For100k' => sub { my $sum = 0; $sum += $_ for 1 .. 100_000; }, 'Map10k' => sub { my $sum = 0; map { $sum += $_ } 1 .. 10_000; }, 'For10k' => sub { my $sum = 0; $sum += $_ for 1 .. 10_000; }, 'Map1k' => sub { my $sum = 0; map { $sum += $_ } 1 .. 1_000; }, 'For1k' => sub { my $sum = 0; $sum += $_ for 1 .. 1_000; }, }); __END__ Rate Map100k For100k Map10k For10k Map1k For1k Map100k 18.6/s -- -19% -90% -92% -99% -99% For100k 23.1/s 24% -- -88% -90% -99% -99% Map10k 187/s 905% 709% -- -22% -91% -92% For10k 239/s 1182% 933% 28% -- -89% -90% Map1k 2077/s 11052% 8880% 1009% 770% -- -13% For1k 2387/s 12715% 10219% 1175% 899% 15% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: When would you choose foreach instead of map?
by Abigail-II (Bishop) on May 21, 2004 at 11:19 UTC | |
by eric256 (Parson) on May 21, 2004 at 15:02 UTC |