I don't know how to check for memory usage (rather than the blunt method of watching the task manager) but here are some speed tests. It appears from this test that they are very simlar in speed for this summing operation. For is the winner but i'm not sure its a noticable portion until you climb into the severl hundered thousand range.
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% --
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.