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.


In reply to map versus for by dHarry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.