Alright, I must apologize, I made an error while trying to use your benchmark. I am sorry about that and about what I said about your tests.

try downloading this and running it on your laptop(and post the results if you dare!)

No need to be very daring, I can recognize a mistake of mine when that occurs. I admit that I was wrong on this point.

How d'you expect code that is eval'd from a string, to see a lexical array?

Of course, code that is eval'd from a string can see a lexical array. Just an example:

use strict; use warnings; my $c = "foo"; my $d = "print q{$c}, q{\n}"; eval ($d); # print "foo" my @f = 1..10; process_f(); sub process_f { my $g = 'print "$_ " for @f; print "\n";'; eval ($g); } my $h = 'my $i = sub { print map {$_ *= 2; "$_ ";} @f; print "\n" }; $ +i->();'; eval ($h);

Here, the problem occurs only because the string is passed to a module function in a separate file, so that the lexical is neither passed as an argument, nor seen as a global. Which is by the way the reason why I don't like using the string version for the Benchmark module and almost never use it. And this is why I made the silly mistake, almost never using this form.

map is faster than for. Just like I said.

I don't think this is true. I've tried your code a dozen times on three different version of Perl, for was almost always faster than map by a very thin margin. As a final test, I increased the number of iterations to 50,000 on Perl 5.18/Windows 7:

use strict; use warnings; use Benchmark; our @array2 = 1 .. 1000; timethese 50000, { "idiomatic" => q[ $_ +=2 for @array2; ], "map" => q[ map $_ +=2, @array2; ], };
Results for 5 runs:
Benchmark: timing 50000 iterations of idiomatic, map... idiomatic: 3 wallclock secs ( 2.87 usr + 0.00 sys = 2.87 CPU) @ 17 +421.60/s ( n=50000) map: 3 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 16 +436.55/s ( n=50000) Benchmark: timing 50000 iterations of idiomatic, map... idiomatic: 3 wallclock secs ( 2.87 usr + 0.00 sys = 2.87 CPU) @ 17 +421.60/s ( n=50000) map: 3 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 16 +436.55/s ( n=50000) Benchmark: timing 50000 iterations of idiomatic, map... idiomatic: 3 wallclock secs ( 2.86 usr + 0.00 sys = 2.86 CPU) @ 17 +513.13/s ( n=50000) map: 3 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 16 +436.55/s ( n=50000) Benchmark: timing 50000 iterations of idiomatic, map... idiomatic: 3 wallclock secs ( 2.87 usr + 0.00 sys = 2.87 CPU) @ 17 +415.53/s ( n=50000) map: 3 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 16 +436.55/s ( n=50000) Benchmark: timing 50000 iterations of idiomatic, map... idiomatic: 3 wallclock secs ( 2.87 usr + 0.00 sys = 2.87 CPU) @ 17 +421.60/s ( n=50000) map: 3 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 16 +436.55/s ( n=50000)

The results are very consistent throughout and it certainly cannot be said that map is faster than for. At best, they are more or less equivalent. But my initial point has never been about performance anyway, but only to suggest a possible explanation for the OP's original question.

Again, I did not want to have a debate with you on this subject in the first place, because I think we were agreeing on most things. And I want to reiterate that I am sorry that I made an error leading me to believe and to say wrongly that your test was wrong.


In reply to Re^8: Write code diferently (A little knowledge is a dangerous thing.) by Laurent_R
in thread Write code diferently by madM

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.