Map has lower overhead than many other list changing algorithms... this is mostly because it uses better, faster, fewer temporary variables. Lets comapare using our good old friend Devel::OpProf.

#!/usr/bin/perl use warnings; use strict; use Devel::OpProf qw'profile print_stats zero_stats'; my @source = ( 1..10_000 ); my @dest = (); #measure the map profile(1); @dest = map { $_ * 10 } @source; profile(0); print "*** map ***\n"; print_stats(); zero_stats(); @dest = (); #measure the foreach profile(1); foreach(@source){ @dest = $_ * 10; } profile(0); print "\n*** foreach ***\n"; print_stats(); zero_stats(); @dest = (); #measure the for profile(1); push @dest, $_ * 10 for @source; profile(0); print "\n*** for ***\n"; print_stats();

The output:

*** map ***
null operation           10005
constant item            10001
scalar variable          10000
map iterator             10000
multiplication (*)       10000
block                    10000
pushmark                 4
next statement           2
private array            2
list assignment          1
map                      1
subroutine entry         1
glob value               1

*** foreach ***
null operation           20005
pushmark                 20002
next statement           20002
glob value               10002
logical and (&&)         10001
private array            10001
constant item            10001
foreach loop iterator    10001
iteration finalizer      10000
multiplication (*)       10000
scalar dereference       10000
list assignment          10000
foreach loop entry       1
subroutine entry         1
loop exit                1

*** for ***
next statement           10003
glob value               10002
pushmark                 10002
logical and (&&)         10001
private array            10001
constant item            10001
foreach loop iterator    10001
multiplication (*)       10000
push                     10000
iteration finalizer      10000
scalar dereference       10000
null operation           5
foreach loop entry       1
subroutine entry         1
loop exit                1

So we see that, a map has less action than a foreach, and stuffing the for in the push is almost as good as a map, and with many of the same operations going on.
--
Snazzy tagline here


In reply to Re: Re (2): Hash/Array of Regular Expressions? (code) by Aighearach
in thread Hash/Array of Regular Expressions? by irom

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.