Skip to content

Instantly share code, notes, and snippets.

@schwern
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save schwern/90caaccb4127cb8a98f3 to your computer and use it in GitHub Desktop.
Save schwern/90caaccb4127cb8a98f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw(cmpthese);
my $String = "ATGC" x 1000;
my @Genes = qw(A T G C);
my %Count;
cmpthese 50_000, {
"eval_tr" => sub {
for my $gene (@Genes) {
$Count{$gene} += eval "\$String =~ tr/$gene//";
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 unrolled_match match eval_tr unrolled_tr
unrolled_match 422/s -- -4% -96% -98%
match 440/s 4% -- -96% -98%
eval_tr 10204/s 2316% 2220% -- -57%
unrolled_tr 23697/s 5511% 5287% 132% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment