in reply to Re^7: Write code diferently (A little knowledge is a dangerous thing.)
in thread Write code diferently

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.

Replies are listed 'Best First'.
Re^9: Write code diferently (A little knowledge is a dangerous thing.)
by BrowserUk (Patriarch) on Aug 29, 2013 at 00:49 UTC
    I am sorry that I made an error

    Thank you, and respect.

    Here are 3 runs each of 5.10.1 (my default installation) and 5.18.0 (self-compiled with MSVC VS v9.0) both running under Vista + SP1 on my hardware:

    C:\test>IdiotsBench.pl Rate idiomatic-my-sub map-my-scope-sub idiomatic-my-sub 10817/s -- -7% map-my-scope-sub 11597/s 7% -- C:\test>IdiotsBench.pl Rate idiomatic-my-sub map-my-scope-sub idiomatic-my-sub 11113/s -- -8% map-my-scope-sub 12126/s 9% -- C:\test>IdiotsBench.pl Rate idiomatic-my-sub map-my-scope-sub idiomatic-my-sub 10760/s -- -11% map-my-scope-sub 12126/s 13% -- C:\test>\perl5.18\bin\perl.exe IdiotsBench.pl Rate map-my-scope-sub idiomatic-my-sub map-my-scope-sub 10426/s -- -9% idiomatic-my-sub 11438/s 10% -- C:\test>\perl5.18\bin\perl.exe IdiotsBench.pl Rate map-my-scope-sub idiomatic-my-sub map-my-scope-sub 10455/s -- -7% idiomatic-my-sub 11269/s 8% -- C:\test>\perl5.18\bin\perl.exe IdiotsBench.pl Rate map-my-scope-sub idiomatic-my-sub map-my-scope-sub 10521/s -- -7% idiomatic-my-sub 11263/s 7% --

    So yes, the balance of power has shifted slightly.

    my initial point has never been about performance anyway, but only to suggest a possible explanation for the OP's original question.

    Which I understood to be so. But, there is no logical reason for denigrating the use of map in a void context.

    Your argument for clarity -- an argument that is very close to my heart -- is false also; because it relies upon the assumption, or presumption, that map *must* return a list; rather than just 'applies a function to a list'.

    You are (relatively) new around here; and I urge you to read and digest the writings of Abigail/Abigail-II, there is little that s/he said on the subject of Perl that isn't worthy if serious note.


    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.