Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

  • You didn't replicate my benchmark correctly. I posted this:
    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:.

    1. a=>q{map {1} @a}

      Where did your get that from? Cos it wasn't my benchmark.

    2. Now the biggy: my @a = 1..1000000;.

      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:

  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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-16 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found