tlm has asked for the wisdom of the Perl Monks concerning the following question:
Hola monks!
I wrote the following snippet to compare various basic sorts:
use strict; use warnings; use Benchmark 'cmpthese'; srand 0; my @n = map int(rand(2**7)), 1..1000; my @a = map chr($_), @n; cmpthese( -1, { default_n => sub { ( sort @n )[ 0 ] }, cmp_n => sub { ( sort { $a cmp $b } @n )[ 0 ] }, '<=>' => sub { ( sort { $a <=> $b } @n )[ 0 ] }, default_a => sub { ( sort @a )[ 0 ] }, cmp_a => sub { ( sort { $a cmp $b } @a )[ 0 ] }, } ); __END__ Rate cmp_n default_n cmp_a default_a <=> cmp_n 667/s -- -0% -3% -3% -40% default_n 667/s 0% -- -3% -3% -40% cmp_a 691/s 4% 4% -- 0% -38% default_a 691/s 4% 4% 0% -- -38% <=> 1110/s 66% 66% 61% 61% --
I'm having a hard time rationalizing why <=> did so much better than the default alphabetic sort, since it was my understanding that the latter should be better. I think the comparison above is fair; am I wrong?
BTW, I figure that the reason why default_n fares worse than default_a is that the former is comparing strings of length up to 3, while the latter is comparing strings of length 1.
the lowliest monk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Yet Another Sort Benchmarking Question
by dave_the_m (Monsignor) on May 08, 2005 at 14:03 UTC | |
by tlm (Prior) on May 08, 2005 at 14:28 UTC | |
by dave_the_m (Monsignor) on May 08, 2005 at 15:16 UTC | |
by tilly (Archbishop) on May 08, 2005 at 19:43 UTC | |
|
Re: Yet Another Sort Benchmarking Question
by salva (Canon) on May 09, 2005 at 09:30 UTC |