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



  • 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.