in reply to Re: Re: = vs cmp
in thread <=> vs cmp
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)
|
|---|