« blib/lib/Benchmark/Perl/Formance/Plugin/Shootout.pm
↑ Summary
blib/lib/Benchmark/Perl/Formance/Plugin/RegexpCommonTS.pm »
| 1 | package Benchmark::Perl::Formance::Plugin::Shootout::regexdna; | ||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: benchmark - Match DNA 8-mers and substitute nucleotides for IUB codes | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # COMMAND LINE: | ||||||||||||||||||||||||||||
| 6 | # /usr/bin/perl regexdna.perl-2.perl 0 < regexdna-input5000000.txt | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # The Computer Language Benchmarks Game | ||||||||||||||||||||||||||||
| 9 | # http://shootout.alioth.debian.org/ | ||||||||||||||||||||||||||||
| 10 | # contributed by Danny Sauer | ||||||||||||||||||||||||||||
| 11 | # completely rewritten and | ||||||||||||||||||||||||||||
| 12 | # cleaned up for speed and fun by Mirco Wahab | ||||||||||||||||||||||||||||
| 13 | # improved STDIN read, regex clean up by Jake Berner | ||||||||||||||||||||||||||||
| 14 | # More speed and multithreading by Andrew Rodland | ||||||||||||||||||||||||||||
| 15 | # Benchmark::Perl::Formance plugin by Steffen Schwigon | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | our $VERSION = "0.002"; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ############################################################# | ||||||||||||||||||||||||||||
| 23 | # # | ||||||||||||||||||||||||||||
| 24 | # Benchmark Code ahead - Don't touch without strong reason! # | ||||||||||||||||||||||||||||
| 25 | # # | ||||||||||||||||||||||||||||
| 26 | ############################################################# | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use File::ShareDir qw(dist_dir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use Benchmark ':hireswallclock'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use Scalar::Util "reftype"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub run | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=38.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 1 | my ($infile) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my $srcdir; eval { $srcdir = dist_dir('Benchmark-Perl-Formance-Cargo')."/Shootout" }; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | return { failed => "no Benchmark-Perl-Formance-Cargo" } if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my $srcfile = "$srcdir/$infile"; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | open my $INFILE, "<", $srcfile or die "Cannot read $srcfile"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $l_file = -s $INFILE; | |||||||||||||||||||||||||||
| 43 | 0 | my $content; read $INFILE, $content, $l_file; | |||||||||||||||||||||||||||
| 44 | # this is significantly faster than using <> in this case | ||||||||||||||||||||||||||||
| 45 | 0 | close $INFILE; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $content =~ s/^>.*//mg; | |||||||||||||||||||||||||||
| 48 | 0 | $content =~ tr/\n//d; | |||||||||||||||||||||||||||
| 49 | 0 | my $l_code = length $content; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my @seq = ( 'agggtaaa|tttaccct', | |||||||||||||||||||||||||||
| 52 | '[cgt]gggtaaa|tttaccc[acg]', | ||||||||||||||||||||||||||||
| 53 | 'a[act]ggtaaa|tttacc[agt]t', | ||||||||||||||||||||||||||||
| 54 | 'ag[act]gtaaa|tttac[agt]ct', | ||||||||||||||||||||||||||||
| 55 | 'agg[act]taaa|ttta[agt]cct', | ||||||||||||||||||||||||||||
| 56 | 'aggg[acg]aaa|ttt[cgt]ccct', | ||||||||||||||||||||||||||||
| 57 | 'agggt[cgt]aa|tt[acg]accct', | ||||||||||||||||||||||||||||
| 58 | 'agggta[cgt]a|t[acg]taccct', | ||||||||||||||||||||||||||||
| 59 | 'agggtaa[cgt]|[acg]ttaccct' ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | my @procs; | |||||||||||||||||||||||||||
| 62 | 0 | for my $s (@seq) { | |||||||||||||||||||||||||||
| 63 | 0 | my $pat = qr/$s/; | |||||||||||||||||||||||||||
| 64 | 0 | my $pid = open my $fh, '-|'; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | defined $pid or die "Error creating process"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | unless ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | my $cnt = 0; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | ++$cnt while $content =~ /$pat/gi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | print "$s $cnt\n"; | |||||||||||||||||||||||||||
| 70 | 0 | exit 0; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | push @procs, $fh; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | for my $proc (@procs) { | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | <$proc>; | |||||||||||||||||||||||||||
| 78 | 0 | close $proc; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my %iub = ( B => '(c|g|t)', D => '(a|g|t)', | |||||||||||||||||||||||||||
| 82 | H => '(a|c|t)', K => '(g|t)', M => '(a|c)', | ||||||||||||||||||||||||||||
| 83 | N => '(a|c|g|t)', R => '(a|g)', S => '(c|g)', | ||||||||||||||||||||||||||||
| 84 | V => '(a|c|g)', W => '(a|t)', Y => '(c|t)' ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # We could cheat here by using $& in the subst and doing it inside a string | ||||||||||||||||||||||||||||
| 87 | # eval to "hide" the fact that we're using $& from the rest of the code... but | ||||||||||||||||||||||||||||
| 88 | # it's only worth 0.4 seconds on my machine. | ||||||||||||||||||||||||||||
| 89 | 0 | my $findiub = '(['.(join '', keys %iub).'])'; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | $content =~ s/$findiub/$iub{$1}/g; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | return { | ||||||||||||||||||||||||||||
| 94 | 0 | length_file => $l_file, | |||||||||||||||||||||||||||
| 95 | length_code => $l_code, | ||||||||||||||||||||||||||||
| 96 | length_content => length $content, | ||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub main | ||||||||||||||||||||||||||||
| 101 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
main: called CC=5 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 1 | my ($options) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | my $goal = $options->{fastmode} ? "fasta-100000.txt" : "fasta-1000000.txt"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | my $result; | |||||||||||||||||||||||||||
| 108 | 1 | ▶ | my $t = timeit $count, sub { $result = run($goal) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | return $result if reftype($result) eq "HASH" and $result->{failed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::reftype($result) eq "HASH" and $result->{"failed"}
MC/DC0%0/2
builtin::reftype($result) eq "HASH" and $result->{"failed"}
builtin::reftype($result) eq "HASH"
$result->{"failed"}
Truth table33%1/3
A and B
A= builtin::reftype($result) eq "HASH"B= $result->{"failed"}
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | return { | ||||||||||||||||||||||||||||
| 112 | 0 | Benchmark => $t, | |||||||||||||||||||||||||||
| 113 | goal => $goal, | ||||||||||||||||||||||||||||
| 114 | count => $count, | ||||||||||||||||||||||||||||
| 115 | result => $result, | ||||||||||||||||||||||||||||
| 116 | }; | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||