Skip to content

Instantly share code, notes, and snippets.

Created July 11, 2013 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5978996 to your computer and use it in GitHub Desktop.
Save anonymous/5978996 to your computer and use it in GitHub Desktop.
#!/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% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment