Let's benchmark:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw 'cmpthese'; our @dates = qw [092205 092305 092605]; our (@mb, @me, @fb, @fe); cmpthese(-1, { map_block => '@mb = @dates; map {s{(\d{2})(\d{2})(\d{2})}{$1/$2/$3 +}} @mb', map_expr => '@me = @dates; map s{(\d{2})(\d{2})(\d{2})}{$1/$2/$3 +}, @me', for_block => '@fb = @dates; for (@fb) {s{(\d{2})(\d{2})(\d{2})}{$1 +/$2/$3}}', for_expr => '@fe = @dates; s{(\d{2})(\d{2})(\d{2})}{$1/$2/$3} for + @fe', }); die unless "@mb" eq "@me" && "@mb" eq "@fb" && "@mb" eq "@fe"; __END__ Rate for_block for_expr map_block map_expr for_block 43115/s -- -1% -2% -6% for_expr 43530/s 1% -- -1% -5% map_block 43840/s 2% 1% -- -5% map_expr 45948/s 7% 6% 5% --
Running it a few more times shows the expression form of map being slightly faster than any of the others, and even the block version of map not doing too bad:
Rate map_block for_expr for_block map_expr map_block 43050/s -- -1% -5% -7% for_expr 43631/s 1% -- -4% -6% for_block 45271/s 5% 4% -- -2% map_expr 46419/s 8% 6% 3% -- Rate for_expr for_block map_block map_expr for_expr 42309/s -- -3% -5% -7% for_block 43530/s 3% -- -3% -5% map_block 44660/s 6% 3% -- -2% map_expr 45583/s 8% 5% 2% -- Rate for_block for_expr map_block map_expr for_block 43115/s -- -1% -3% -7% for_expr 43530/s 1% -- -2% -6% map_block 44246/s 3% 2% -- -5% map_expr 46419/s 8% 7% 5% --
Things might be different on your machine, but the difference in performance is too small to regard one as better than the other.

Now, if you really want to go for speed, your concern should not be the difference between 'for' and 'map', but the difference between 's///' and something else. For instance, let's introduce an additional clause in the benchmark:

pack => '@pk = @dates; @pk = map {join "/", unpack "A2 A2 A2", $_} @pk +'
This leads to the results:
Rate for_expr for_block map_block map_expr pack for_expr 42309/s -- -0% -2% -9% -48% for_block 42309/s -0% -- -2% -9% -48% map_block 43115/s 2% 2% -- -7% -47% map_expr 46394/s 10% 10% 8% -- -43% pack 80736/s 91% 91% 87% 74% --
It dwarves the difference between map and for.

In reply to Re^6: modify the contents of an array by Anonymous Monk
in thread modify the contents of an array by s_gaurav1091

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.