Your benchmark, on which you base all your demonstration that I am wrong and that you are right, is just hot air and pure baloney. And it is plain wrong.

Wrong! All you've done is show how little you understand about Perl.

Multiplying a value by 1 is not terribly useful, and part of your code is probably optimized away.

Again, you show your lack of knowledge. Multiplying by one does not get optimised away.

Now, let's do a real benchmark:

You're joking right? Because your benchmark is crap.

You are timing how long it takes:

  1. to allocate an array;
  2. and then populate it;
  3. and the time taken to call a subroutine (the one you wrote) from within a subroutine (the one Benchmark wraps around the code ref (or string) you give to it.)
  4. in addition to the time taken to iterate it..

Let's start with your benchmark as posted (minus the eclectic formatting), run on my machine:

timethese 10000, { "idiomatic" => sub { my @array = 1..1000; $_ +=2 for @array; +}, "map" => sub { my @array = 1..1000; map{ $_ +=2;} @array; +}, }; __END__ C:\test>IdiotsBench.pl Benchmark: timing 10000 iterations of idiomatic, map... idiomatic: 2 wallclock secs ( 1.52 usr + 0.00 sys = 1.52 CPU) @ 65 +96.31/s (n=10000) map: 3 wallclock secs ( 3.06 usr + 0.00 sys = 3.06 CPU) @ 32 +64.77/s (n=10000)

Now let's remove those array allocations and populations from the timing:

C:\test>IdiotsBench.pl my @array = 1..1000; timethese 10000, { "idiomatic" => sub { $_ +=2 for @array; }, "map" => sub { map{ $_ +=2 } @array; }, }; __END__ C:\test>IdiotsBench.pl Benchmark: timing 10000 iterations of idiomatic, map... idiomatic: 1 wallclock secs ( 0.92 usr + 0.00 sys = 0.92 CPU) @ 10 +845.99/s (n=10000) map: 2 wallclock secs ( 2.44 usr + 0.00 sys = 2.44 CPU) @ 41 +01.72/s (n=10000)

Now let's remove the expensive and redundant block scope from the map test:

C:\test>IdiotsBench.pl timethese 10000, { "idiomatic" => sub { $_ +=2 for @array; }, "map" => sub { map $_ +=2, @array; }, }; __END__ Benchmark: timing 10000 iterations of idiomatic, map... idiomatic: 1 wallclock secs ( 0.95 usr + 0.00 sys = 0.95 CPU) @ 10 +493.18/s (n=10000) map: 1 wallclock secs ( 0.95 usr + 0.00 sys = 0.95 CPU) @ 10 +493.18/s (n=10000)

And finally, the double function call:

our @array2 = 1 .. 1000; timethese 10000, { "idiomatic" => q[ $_ +=2 for @array2; ], "map" => q[ map $_ +=2, @array2; ], }; __END__ Benchmark: timing 10000 iterations of idiomatic, map... idiomatic: 1 wallclock secs ( 0.88 usr + 0.00 sys = 0.88 CPU) @ 11 +428.57/s (n=10000) map: 1 wallclock secs ( 0.84 usr + 0.00 sys = 0.84 CPU) @ 11 +848.34/s (n=10000)

And waddayaknow; now we are benchmarking just the disputed code and not conflating it with a bunch other random stuff, map is faster than for. Just like I said.

You want to withdraw your accusation of phony results now?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^7: Write code diferently (A little knowledge is a dangerous thing.) by BrowserUk
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.