« blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/pidigits.pm
↑ Summary
blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/mandelbrot.pm »
| 1 | package Benchmark::Perl::Formance::Plugin::Shootout::knucleotide; | ||
| 2 | our $AUTHORITY = 'cpan:SCHWIGON'; | ||
| 3 | # ABSTRACT: benchmark - Hashtable update and k-nucleotide strings | ||
| 4 | |||
| 5 | # COMMAND LINE: | ||
| 6 | # /usr/bin/perl knucleotide.perl 0 < knucleotide-input25000000.txt | ||
| 7 | |||
| 8 | # The Computer Language Benchmarks Game | ||
| 9 | # http://shootout.alioth.debian.org/ | ||
| 10 | |||
| 11 | # contributed by Karl FORNER | ||
| 12 | # (borrowed fasta loading routine from Kjetil Skotheim, 2005-11-29) | ||
| 13 | # Corrected again by Jesse Millikan | ||
| 14 | # revised by Kuang-che Wu | ||
| 15 | # Multi-threaded by Andrew Rodland | ||
| 16 | # Benchmark::Perl::Formance plugin by Steffen Schwigon | ||
| 17 | |||
| 18 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | |||
| 22 | our $VERSION = "0.001"; | ||
| 23 | |||
| 24 | ############################################################# | ||
| 25 | # # | ||
| 26 | # Benchmark Code ahead - Don't touch without strong reason! # | ||
| 27 | # # | ||
| 28 | ############################################################# | ||
| 29 | |||
| 30 | use File::ShareDir qw(dist_dir); | ||
| 31 | use Benchmark ':hireswallclock'; | ||
| 32 | use Scalar::Util "reftype"; | ||
| 33 | |||
| 34 | our $PRINT = 0; | ||
| 35 | |||
| 36 | my $threads; | ||
| 37 | my ($l,%h,$sum); | ||
| 38 | my ($sequence, $begin, $end); | ||
| 39 | my $output; | ||
| 40 | |||
| 41 | sub run | ||
| 42 | { | ||
| 43 | my ($infile) = @_; | ||
| 44 | |||
| 45 | $output = ''; | ||
| 46 | $threads = 2*num_cpus() || 1; | ||
| 47 | |||
| 48 | my $srcdir; eval { $srcdir = dist_dir('Benchmark-Perl-Formance-Cargo')."/Shootout" }; | ||
| 49 | return { failed => "no Benchmark-Perl-Formance-Cargo" } if $@; | ||
| 50 | |||
| 51 | my $srcfile = "$srcdir/$infile"; | ||
| 52 | open my $INFILE, "<", $srcfile or die "Cannot read $srcfile"; | ||
| 53 | |||
| 54 | $/ = ">"; | ||
| 55 | /^THREE/ and $sequence = uc(join "", grep !/^THREE/, split /\n+/) while <$INFILE>; | ||
| 56 | |||
| 57 | close $INFILE; | ||
| 58 | |||
| 59 | ($l,%h,$sum) = (length $sequence); | ||
| 60 | |||
| 61 | foreach my $frame (1,2) { | ||
| 62 | %h = (); | ||
| 63 | update_hash_for_frame($frame); | ||
| 64 | $sum = $l - $frame + 1; | ||
| 65 | $output .= sprintf "$_ %.3f\n", $h{$_}*100/$sum for sort { $h{$b} <=> $h{$a} || $a cmp $b } keys %h; | ||
| 66 | $output .= "\n"; | ||
| 67 | } | ||
| 68 | |||
| 69 | foreach my $s (qw(GGT GGTA GGTATT GGTATTTTAATT GGTATTTTAATTTATAGT)) { | ||
| 70 | update_hash_for_frame(length($s)); | ||
| 71 | $output .= sprintf "%d\t$s\n", $h{$s}; | ||
| 72 | } | ||
| 73 | |||
| 74 | print $output if $PRINT; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub update_hash_for_frame { | ||
| 78 | my $frame = $_[0]; | ||
| 79 | my @threads; | ||
| 80 | for my $i (0 .. $threads - 1) { | ||
| 81 | use integer; | ||
| 82 | my $begin = $l * $i / $threads; | ||
| 83 | my $end = $l * ($i + 1) / $threads - 1; | ||
| 84 | no integer; | ||
| 85 | if ($end > $l - $frame) { | ||
| 86 | $end = $l - $frame; | ||
| 87 | } | ||
| 88 | push @threads, threads->create(\&update_hash_slice, $frame, $begin, $end); | ||
| 89 | } | ||
| 90 | for my $thread (@threads) { | ||
| 91 | my $count = $thread->join; | ||
| 92 | $h{$_} += $count->{$_} for keys %$count; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub update_hash_slice { | ||
| 97 | my ($frame, $begin, $end) = @_; | ||
| 98 | my %local; | ||
| 99 | $local{substr($sequence,$_,$frame)}++ for $begin .. $end; | ||
| 100 | return \%local; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub num_cpus { | ||
| 104 | open my $fh, '<', '/proc/cpuinfo' or return; | ||
| 105 | my $cpus; | ||
| 106 | while (<$fh>) { | ||
| 107 | $cpus ++ if /^processor[\s]+:/; # 0][]0]; # for emacs cperl-mode indent bug | ||
| 108 | } | ||
| 109 | return $cpus; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub main | ||
| 113 | { | ||
| 114 | my ($options) = @_; | ||
| 115 | |||
| 116 | $PRINT = $options->{D}{Shootout_knucleotide_print}; | ||
| 117 | my $goal = $options->{fastmode} ? "fasta-25000.txt" : "fasta-1000000.txt"; | ||
| 118 | my $count = $options->{fastmode} ? 1 : 5; | ||
| 119 | |||
| 120 | my $result; | ||
| 121 | my $t = timeit $count, sub { $result = run($goal) }; | ||
| 122 | return $result if reftype($result) eq "HASH" and $result->{failed}; | ||
| 123 | |||
| 124 | return { | ||
| 125 | Benchmark => $t, | ||
| 126 | goal => $goal, | ||
| 127 | count => $count, | ||
| 128 | result => $result, | ||
| 129 | threads => $threads, | ||
| 130 | l => $l, | ||
| 131 | sum => $sum, | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | __END__ | ||