Dear Monks
I was contemplating over the 256 commandments from Damian’s "Perl Best Practices" when I encountered:
List Generation
Use map instead of for when generating new lists from old.
Being more used to other programming languages I often use the non-Perl approach to get the job done. I would typically use a for and probably not even consider the map. So this was an eye-opener for me. I decided to do a little test to see how much the difference is between the two.
(FYI: Perl v5.8.8 built for MSWin32-x86-multi-thread running on a Dell INSPIRON 9400)
I use the example as mentioned by Damian and the Benchmark module to test:
use strict;
use warnings;
use Benchmark qw(:all);
my @results;
my $count = -5;
# Populate list with 10 mio numbers
for (my $i=0; $i<1000_000; $i++) {
push @results, $i;
}
cmpthese (
$count,
{
for => "test_for;",
map => "test_map;",
}
);
timethese($count, {
for => "test_for;",
map => "test_map;",
}
);
sub test_for {
my @sqrt_results;
for my $result (@results) {
push @sqrt_results , sqrt($result);
}
}
sub test_map {
my @sqrt_results = map { sqrt $_ } @results;
}
First the comparison:
$count=-1
(warning: too few iterations for a reliable count)
Rate for map
for 2.67/s -- -10%
map 2.98/s 12% --
$count=-5
Rate map for
map 3.05/s -- -16%
for 3.61/s 18% --
$count=-10
Rate for map
for 2.73/s -- -8%
map 2.95/s 8% --
Hmmm, not really impressive this “gain” of using map over for?!
Next some timing:
$count=-1
Benchmark: running for, map for at least 1 CPU seconds...
for: 1 wallclock secs ( 1.16 usr + 0.00 sys = 1.16 CPU) @ 3
+.46/s (n=4)
map: 2 wallclock secs ( 1.19 usr + 0.00 sys = 1.19 CPU) @ 3
+.37/s (n=4)
$count=-5
Benchmark: running for, map for at least 5 CPU seconds...
for: 6 wallclock secs ( 5.22 usr + 0.00 sys = 5.22 CPU) @ 3
+.64/s (n=19)
map: 6 wallclock secs ( 5.22 usr + 0.00 sys = 5.22 CPU) @ 3
+.45/s (n=18)
$count=-10
Benchmark: running for, map for at least 10 CPU seconds...
for: 10 wallclock secs (10.11 usr + 0.00 sys = 10.11 CPU) @ 3
+.46/s (n=35)
map: 10 wallclock secs (10.03 usr + 0.00 sys = 10.03 CPU) @ 3
+.29/s (n=33)
Am I missing something? Is the example given by Damian a poor example? Should I really favor map over for when I want to generate a new list from another list?
Thanks upfront
Update
Beside the obvious advantages: less code, easier to understand, it is stated that map is normally considerably faster.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.