Skip to content

Instantly share code, notes, and snippets.

@oklahomer
Created February 16, 2014 05:19
Show Gist options
  • Save oklahomer/9029685 to your computer and use it in GitHub Desktop.
Save oklahomer/9029685 to your computer and use it in GitHub Desktop.
benchmarked each sort style provided by Sort::Maker for my specific purpose
#!/usr/bin/env perl
use strict;
use warnings;
use Sort::Maker qw/make_sorter/;
use Benchmark qw/:all/;
warn "Perl: $]\n";
warn "Sort::Maker: $Sort::Maker::VERSION\n\n";
#Perl: 5.018001
#Sort::Maker: 0.06
#
# Rate plain original orcish ST GRT
#plain 6.83/s -- -79% -82% -85% -86%
#original 33.2/s 386% -- -13% -25% -34%
#orcish 38.4/s 462% 16% -- -14% -23%
#ST 44.5/s 551% 34% 16% -- -11%
#GRT 50.2/s 635% 51% 31% 13% --
my $hashref = +{
status => 1,
error_msg => '',
result => +{},
};
$hashref->{result}->{ sprintf('foo%d', $_) } = rand(100) for 1..5_000;
my $sort_rule = +{
number => +{
code => sub { /\A foo(\d+) \z/xms },
ascending => 1,
}
};
my $sorter_plain = make_sorter( plain => 1, %$sort_rule );
my $sorter_orc = make_sorter( orcish => 1, %$sort_rule );
my $sorter_st = make_sorter( ST => 1, %$sort_rule );
my $sorter_grt = make_sorter( GRT => 1, %$sort_rule );
my $sorter_orig = sub {
my %tmp_cache;
sort {
( $tmp_cache{$a} //= $a =~ s/\A foo(\d+) \z/$1/xr ) <=>
( $tmp_cache{$b} //= $b =~ s/\A foo(\d+) \z/$1/xr )
} @_;
};
cmpthese(
1_000,
+{
plain => sub {
my @sorted_keys = $sorter_plain->( keys %{ $hashref->{result} } );
},
orcish => sub {
my @sorted_keys = $sorter_orc->( keys %{ $hashref->{result} } );
},
ST => sub {
my @sorted_keys = $sorter_st->( keys %{ $hashref->{result} } );
},
GRT => sub {
my @sorted_keys = $sorter_grt->( keys %{ $hashref->{result} } );
},
original => sub {
my @sorted_keys = $sorter_orig->( keys %{ $hashref->{result} } );
},
},
);
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment