in reply to Re^6: Write code diferently
in thread Write code diferently
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.
cmpthese -3,{a=>q[@a = map $_*1, @a],b=>q[map $_*=1, @a],c=>q[$_*=1 fo +r @a] };;
You used this:
timethese (10000000,{a=>q{map {1} @a},b=>q[map $_*=1, @a],c=>q[$_*=1 f +or @a] });
Spot your idiocies:.
Where did your get that from? Cos it wasn't my benchmark.
How d'you expect code that is eval'd from a string, to see a lexical array?
Since you seem incapable of understanding stuff that requires a little knowledge; try downloading this and running it on your laptop(and post the results if you dare!):
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:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Write code diferently (A little knowledge is a dangerous thing.)
by Laurent_R (Canon) on Aug 29, 2013 at 00:18 UTC | |
by BrowserUk (Patriarch) on Aug 29, 2013 at 00:49 UTC |