Skip to content

Instantly share code, notes, and snippets.

@schwern
Created December 19, 2014 00:27
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 schwern/93d94e987c7bf5bccaf4 to your computer and use it in GitHub Desktop.
Save schwern/93d94e987c7bf5bccaf4 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 10_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;
die $@ if $@;
}
},
"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;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment