in reply to Re: sort routines crossing package lines
in thread sort routines crossing package lines
Update: I'm now saving the values for more realism, and corrected the extra 's' BrowserUK mentioned (which I had already fixed in the .pl file, just not in the post).
Some concrete numbers:
use Benchmark (); sub argless { $a cmp $b } sub proto($$) { $_[0] cmp $_[1] } sub args { $_[0] cmp $_[1] } my @array = qw( Perl may be copied only under the terms of either the Artistic Lice +nse or the GNU General Public License, which may be found in the Perl 5 source + kit. ); Benchmark::cmpthese(0, { inline => sub { my @sorted = sort { $a cmp $b } @array; }, argless => sub { my @sorted = sort argless @array; }, proto => sub { my @sorted = sort proto @array; }, args => sub { my @sorted = sort { args($a, $b) } @array; }, }); __END__ Rate args proto argless inline args 4634/s -- -57% -63% -81% proto 10735/s 132% -- -14% -57% argless 12462/s 169% 16% -- -50% inline 24920/s 438% 132% 100% --
Here's a different comparison: (One that yeilds less unfair optimization)
use Benchmark (); sub argless { $hash{$a } cmp $hash{$b } } sub proto($$) { $hash{$_[0]} cmp $hash{$_[1]} } sub args { $hash{$_[0]} cmp $hash{$_[1]} } our %hash = map { (''.rand()) => $_ } qw( Perl may be copied only under the terms of either the Artistic Lice +nse or the GNU General Public License, which may be found in the Perl 5 source + kit. ); Benchmark::cmpthese(0, { inline => sub { my @sorted = sort { $hash{$a} cmp $hash{$b} } k +eys %hash; }, argless => sub { my @sorted = sort argless k +eys %hash; }, proto => sub { my @sorted = sort proto k +eys %hash; }, args => sub { my @sorted = sort { args($a, $b) } k +eys %hash; }, }); __END__ Rate args proto argless inline args 2832/s -- -33% -42% -43% proto 4258/s 50% -- -13% -14% argless 4871/s 72% 14% -- -2% inline 4973/s 76% 17% 2% --
Active Perl 5.6.1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: sort routines crossing package lines
by BrowserUk (Patriarch) on Nov 24, 2004 at 23:37 UTC | |
|
Re^3: sort routines crossing package lines
by DrWhy (Chaplain) on Nov 24, 2004 at 23:37 UTC | |
|
Re^3: sort routines crossing package lines
by BrowserUk (Patriarch) on Nov 24, 2004 at 23:14 UTC |