Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Well, you are not measuring anything but how fast Perl can call a subroutine. The very high iterations/per second for sorting a 200 element array should have tipped you off.

One should realize that Benchmark calls your function in void context. And sorting in void context is optimized to not do the sorting. So, you haven't benchmark anything. Benchmarking is an art, and you shouldn't test something with one size of data and draw conclusions.

Here's a better benchmark:

use strict; use Benchmark; for (my $size = 10; $size <= 100_000; $size *= 10) { @main::array = map {int rand $size} 1 .. $size; timethese -3 => { "[$size] <=>" => 'my @array2 = sort {$a <=> $b} @main::array +', "[$size] cmp" => 'my @array2 = sort {$a cmp $b} @main::array +', } }
Name "main::array" used only once: possible typo at ./sort_bench.pl line 9.
Benchmark: running 10 <=>, 10 cmp, each for at least 3 CPU seconds...
  10 <=>:  4 wallclock secs ( 3.13 usr +  0.01 sys =  3.14 CPU) @ 45414.01/s (n=142600)
  10 cmp:  3 wallclock secs ( 3.01 usr +  0.00 sys =  3.01 CPU) @ 44175.08/s (n=132967)
Benchmark: running 100 <=>, 100 cmp, each for at least 3 CPU seconds...
 100 <=>:  3 wallclock secs ( 3.23 usr +  0.01 sys =  3.24 CPU) @ 4453.70/s (n=14430)
 100 cmp:  3 wallclock secs ( 3.23 usr +  0.00 sys =  3.23 CPU) @ 4095.67/s (n=13229)
Benchmark: running 1000 <=>, 1000 cmp, each for at least 3 CPU seconds...
1000 <=>:  3 wallclock secs ( 3.22 usr +  0.01 sys =  3.23 CPU) @ 324.77/s (n=1049)
1000 cmp:  3 wallclock secs ( 3.26 usr +  0.00 sys =  3.26 CPU) @ 292.64/s (n=954)
Benchmark: running 10000 <=>, 10000 cmp, each for at least 3 CPU seconds...
10000 <=>:  3 wallclock secs ( 3.09 usr +  0.01 sys =  3.10 CPU) @ 17.74/s (n=55)
10000 cmp:  3 wallclock secs ( 3.08 usr +  0.00 sys =  3.08 CPU) @ 13.96/s (n=43)
Benchmark: running 100000 <=>, 100000 cmp, each for at least 3 CPU seconds...
100000 <=>:  4 wallclock secs ( 3.42 usr +  0.01 sys =  3.43 CPU) @  1.17/s (n=4)
100000 cmp:  3 wallclock secs ( 3.30 usr +  0.01 sys =  3.31 CPU) @  0.91/s (n=3)
            (warning: too few iterations for a reliable count)

In reply to Re: = vs cmp by Anonymous Monk
in thread &lt;=&gt; vs cmp by Jasper

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 examining the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found