Skip to content

Instantly share code, notes, and snippets.

/benchmark.pl Secret

Created July 16, 2013 17:21
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 anonymous/2914d6494253eec53f91 to your computer and use it in GitHub Desktop.
Save anonymous/2914d6494253eec53f91 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw(:all) ;
my $line = "ACGT" x 1_000_000;
my @order = qw(A C G T);
sub counting_with_regexp {
my %nucleotides = (
A => 0,
C => 0,
G => 0,
T => 0,
);
for my $letter ( keys( %nucleotides ) ) {
$nucleotides{$letter} += ( () = $line =~ /$letter/g );
}
}
sub counting_with_substr {
my %nucleotides;
for my $i (0..length($line)) {
my $char = substr($line,$i,1);
$nucleotides{$char}++;
}
}
timethese(10, {
"counting with regexp" => \&counting_with_regexp,
"counting with substr" => \&counting_with_substr,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment