public
anonymous / gist:5978996
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#!/usr/bin/perl
 
use Benchmark qw( cmpthese );
sub nostcmp {
my ($a_char, $a_num) = split '_', $a;
my ($b_char, $b_num) = split '_', $b;
return $a_char cmp $b_char
||
$a_num <=> $b_num;
}
 
sub nost {
return sort nostcmp @_;
}
sub st {
return
map $_->[0],
sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] }
map [ $_, split /_/ ],
@_;
}
 
 
my %tests = (
nost => 'my @sorted = nost(@list);',
st => 'my @sorted = st(@list);',
);
 
 
$_ = 'use strict; use warnings; our @list; ' . $_
for values %tests;
 
{
local our @list = qw(B_5 A_11 C_0 A_10 A_1);
cmpthese(-3, \%tests);
}
 
{
local our @list = qw(B_5 A_11 C_0 A_10 A_1);
@lists = (@lists)x100;
cmpthese(-3, \%tests);
}
 
__END__
Rate st nost
st 92589/s -- -7%
nost 99653/s 8% --
 
Rate st nost
st 93349/s -- -6%
nost 99817/s 7% --

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.