blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/pidigits.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« blib/lib/Benchmark/Perl/Formance/Plugin/AccessorsMouse.pm ↑ Summary blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/knucleotide.pm »
1 package Benchmark::Perl::Formance::Plugin::Shootout::pidigits;
2 our $AUTHORITY = 'cpan:SCHWIGON';
3 # ABSTRACT: benchmark - Streaming arbitrary-precision arithmetic
4  
5 # The Computer Language Benchmarks Game
6 # http://shootout.alioth.debian.org/
7 #
8 # contributed by Robert Bradshaw
9 # modified by Ruud H.G.van Tol
10 # modified by Emanuele Zeppieri
11 # modified to use Math:GMP by Kuang-che Wu
12 # Benchmark::Perl::Formance plugin by Steffen Schwigon
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use Math::GMP;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Benchmark ':hireswallclock';
17  
18 our $VERSION = "0.001";
19  
20 #############################################################
21 # #
22 # Benchmark Code ahead - Don't touch without strong reason! #
23 # #
24 #############################################################
25  
26 my($z0, $z1, $z2);
27  
28 sub extract_digit { return ($z0*$_[0]+$z1)/$z2; }
29  
30 sub compose {
31 if ( defined $_[3] ) {
32 $z1 = $z1*$_[0]+$_[1]*$z2;
33 } else {
34 $z1 = $z1*$_[2]+$_[1]*$z0;
35 }
36 $z0 = $z0*$_[0];
37 $z2 = $z2*$_[2];
38 return;
39 }
40  
41 sub run
42 {
43 my $output = '';
44  
45 ($z0, $z1, $z2) = map Math::GMP->new($_),1,0,1;
46  
47 my $n = $_[0];
48 local ($,, $\) = ("\t", "\n");
49 my ($i, $s, $d); my $k = 0;
50  
51 # main loop
52 for $i (1..$n) { ## no critic
53 while (
54 $z0>$z2 || ( $d = extract_digit(3) ) != extract_digit(4)
55 ) {
56 # y not safe
57 $k++; compose($k, 4*$k+2, 2*$k+1)
58 }
59 compose(10, -10*$d, 1, 1);
60 $s .= $d;
61  
62 unless ( $i % 10 ) {
63 $output .= $s; undef $s;
64 }
65 }
66  
67 $s .= ' ' x (10-$i) if $i = $n % 10;
68  
69 $output .= $s if $s;
70 return $output;
71 }
72  
73 sub main
74 {
75 my ($options) = @_;
76  
77 my $goal = $options->{fastmode} ? 100 : 20_000;
78 my $count = $options->{fastmode} ? 1 : 5;
79  
80 my $result;
81 my $t = timeit $count, sub { $result = run($goal) };
82 return {
83 Benchmark => $t,
84 goal => $goal,
85 count => $count,
86 result => substr($result, 0, $goal <= 10 ? $goal : 10)."[...]",
87 };
88 }
89  
90 1;
91  
92 __END__
« blib/lib/Benchmark/Perl/Formance/Plugin/AccessorsMouse.pm ↑ Summary blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/knucleotide.pm »