I tested, and map is significantly faster than for or foreach at large numbers of iterations

I'm sorry, I know it's off-topic, but I'd really like to see the Benchmarks to back this statement up. It's not that I don't believe you, just that it would be really surprising to me if it were true.

Update: well, I am indeed surprised. Here's a benchmark I cooked up:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese timethese); sub tp_map { my $i; my $pos = tell DATA; while (<DATA>) { chomp; chop; s/\?'/'/g; $i = 0; map { '['.$i++."]$_\n" } split(/\+/); } seek DATA, $pos, 0; } sub tp_for { my $i; my $pos = tell DATA; while (<DATA>) { chomp; chop; s/\?'/'/g; $i = 0; '['.$i++."]$_\n" for split(/\+/); } seek DATA, $pos, 0; } cmpthese timethese(-2, { tp_map => \&tp_map, tp_for => \&tp_for, }); __DATA__ 0010+2+O?'Reilly' 012+90+Penguin'

And here are the results:

Benchmark: running tp_for, tp_map for at least 2 CPU seconds... tp_for: 2 wallclock secs ( 1.92 usr + 0.18 sys = 2.10 CPU) @ 48 +185.71/s (n=101190) tp_map: 2 wallclock secs ( 1.90 usr + 0.17 sys = 2.07 CPU) @ 52 +882.61/s (n=109467) Rate tp_for tp_map tp_for 48186/s -- -9% tp_map 52883/s 10% --

Which shows that map is indeed faster for something like this. Of course, the rates for both are still quite high, and actually choosing between map and for based on speed seems silly, but I'm still surprised.


In reply to Re^2: Improved regexp sought by revdiablo
in thread Improved regexp sought by myomancer

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.