Skip to content

Instantly share code, notes, and snippets.

@mpapec
Forked from schwern/gist:90caaccb4127cb8a98f3
Last active August 29, 2015 14:11
Show Gist options
  • Save mpapec/29ec920803da760a1bd8 to your computer and use it in GitHub Desktop.
Save mpapec/29ec920803da760a1bd8 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Benchmark qw(cmpthese);
my $String = "ATGC" x 1000;
my @Genes = qw(A T G C);
my %Count;
my %dispatch = map { $_ => eval "sub { \$_[0] =~ tr/$_// }" } @Genes;
cmpthese -3, {
"eval_tr" => sub {
for my $gene (@Genes) {
$Count{$gene} += eval "\$String =~ tr/$gene//";
die $@ if $@;
}
},
"eval_sub" => sub {
for my $gene (@Genes) {
$Count{$gene} += $dispatch{$gene}->($String);
# die $@ if $@;
}
},
"match" => sub {
for my $gene (@Genes) {
$Count{$gene} += () = $String =~ /$gene/g;
}
},
"unrolled_tr" => sub {
$Count{A} += $String =~ tr/A//;
$Count{T} += $String =~ tr/T//;
$Count{G} += $String =~ tr/G//;
$Count{C} += $String =~ tr/C//;
},
"unrolled_match" => sub {
$Count{A} += () = $String =~ /A/g;
$Count{T} += () = $String =~ /T/g;
$Count{G} += () = $String =~ /G/g;
$Count{C} += () = $String =~ /C/g;
},
};
__END__
Rate match unrolled_match eval_tr eval_sub unrolled_tr
match 617/s -- -2% -96% -99% -99%
unrolled_match 628/s 2% -- -96% -99% -99%
eval_tr 14111/s 2186% 2147% -- -71% -73%
eval_sub 48849/s 7813% 7679% 246% -- -8%
unrolled_tr 53249/s 8525% 8380% 277% 9% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment