Skip to content

Instantly share code, notes, and snippets.

@ichesnokov
Created November 24, 2015 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichesnokov/7feea4c15a868d48e5a0 to your computer and use it in GitHub Desktop.
Save ichesnokov/7feea4c15a868d48e5a0 to your computer and use it in GitHub Desktop.
map-for-foreach speed comparison
perl -MBenchmark=cmpthese -E 'cmpthese(10000, { for => sub { my $i = 0; for (1..1000) { $i += $_; } }, map => sub { my $i=0; map { $i += $_; } 1..1000 }, foreach => sub { my $i=0; foreach (1..1000) { $i+=$_; } }, postfix_for => sub { my $i=0; $i+=$_ for 1..1000; }, postfix_foreach => sub { my $i = 0; $i += $_ foreach 1..1000; } });'
Rate map postfix_foreach for foreach postfix_for
map 12195/s -- -27% -27% -29% -33%
postfix_foreach 16667/s 37% -- -0% -3% -8%
for 16667/s 37% 0% -- -3% -8%
foreach 17241/s 41% 3% 3% -- -5%
postfix_for 18182/s 49% 9% 9% 5% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment