Created
July 11, 2013 20:33
-
-
Save anonymous/5978996 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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