Skip to content

Instantly share code, notes, and snippets.

@tryperl
Created February 12, 2013 09:36
Show Gist options
  • Save tryperl/4761202 to your computer and use it in GitHub Desktop.
Save tryperl/4761202 to your computer and use it in GitHub Desktop.
Created by www.tryperl.com.
use Data::Dumper;
use Benchmark qw(:all) ;
my @anim = ('rn4,mm8,bosTau2,canFam2,dasNov1,echTel1',
'rn4,mm8,oryCun1,bosTau2,canFam2,dasNov1,echTel1');
my @bigarray = ('rn4,mm8,bosTau2,canFam2,dasNov1,echTel1' x 1000, 'rn4,mm8,oryCun1,bosTau2,canFam2,dasNov1,echTel1' x 1000, 'short' x 1000);
my @randoma;
for my $i (1..100){
my $r = 1+int(rand(90));
push(@bigarray,@anim,$anim[1] x $i,$anim[0] x $r);
}
#die Dumper(\@bigarray);
my $comma_re = qr!,!;
#print "b: ".($anim[0] =~ s!$comma_re!!g)."a: ".($anim[1] =~ s!$comma_re!!g)."\n";
timethese(100, {
'sort_hash_small' => \&sort_hash_small,
'sort_hash_big' => \&sort_hash_big,
'sort_re_small' => \&sort_re_small,
'sort_re_big' => \&sort_re_big,
});
sub sort_hash_small {
return sort_hash(@anim);
}
sub sort_hash_big {
return sort_hash(@bigarray);
}
sub sort_re_small{
return sort_re(@anim);
}
sub sort_re_big{
return sort_re(@bigarray);
}
sub sort_hash {
my @a=@_;
my %hash;
my @sort_a;
for(my $i=0;$i<scalar(@a);$i++)
{
$hash{scalar(split(",",$a[$i]))}=$a[$i];
}
foreach(sort keys %hash)
{
push(@sort_a,$hash{$_});
}
return @sort_a;
}
sub sort_re {
my @tmp = @_;
my @animsort = sort{$b =~ s!$comma_re!!g <=> $a =~ s!$comma_re!!g} @tmp;
return @animsort;
}
@gideondsouza
Copy link

whoever created this, I'm working on a way to make Data::Dumper usable alright ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment