PerlStone2015/binarytrees.pm

filter: Statementstmt 29.4% 15 / 51 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod 0.0% 0 / 4 totaltotal 25.6% 19 / 74 CC 10 SCAR 24.8
CC
10
Cov
25%
bottom_up_tree
20.0
run
20.0
main
12.0
CRAP 11.9
« PerlStone2015/spectralnorm.pm ↑ Summary PerlStone2015/mandelbrot.pm »
1 package Benchmark::Perl::Formance::Plugin::PerlStone2015::binarytrees;
2 1 our $AUTHORITY = 'cpan:SCHWIGON';
3 # ABSTRACT: benchmark - Allocate and deallocate many many binary trees
4 1 $Benchmark::Perl::Formance::Plugin::PerlStone2015::binarytrees::VERSION = '0.002';
5 # COMMAND LINE:
6 # /usr/bin/perl binarytrees.perl-2.perl 20
7  
8 # The Computer Language Benchmarks Game
9 # http://shootout.alioth.debian.org/
10 #
11 # Contributed by Doug King
12 # Corrected by Heiner Marxen
13 # Tree-building made non-recursive by Steffen Mueller
14 # Benchmark::Perl::Formance plugin by Steffen Schwigon
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Benchmark ':hireswallclock';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #############################################################
22 # #
23 # Benchmark Code ahead - Don't touch without strong reason! #
24 # #
25 #############################################################
26  
27 0 ▶sub item_check {
Subroutine item_check: not called CC=2 SCAR=17.9
Pod undocumented
28 0 my ($tree) = @_;
29  
30 0 ▶ return $tree->[2] unless (defined $tree->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree->[0] 0 0 0
31 0 return $tree->[2] + item_check($tree->[0]) - item_check($tree->[1]);
32 }
33  
34  
35 0 ▶sub bottom_up_tree {
Subroutine bottom_up_tree: not called CC=4 SCAR=30.0
Pod undocumented
36 0 my($depth) = @_;
37  
38 0 my @pool;
39 0 push @pool, [undef, undef, -$_] foreach 0..2**$depth-1;
40  
41 0 foreach my $exponent (reverse(0..($depth-1))) {
42 push @pool, [reverse(splice(@pool, 0, 2)), $_]
43 0 foreach reverse(-(2**$exponent-1) .. 0);
44 }
45 0 return $pool[0];
46 }
47  
48 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
49 0 my ($n) = @_;
50  
51 0 my $min_depth = 4;
52 0 my $max_depth;
53  
54 0 ▶ if ( ($min_depth + 2) > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_depth + 2 > $n) { } 0 0 0
55 0 $max_depth = $min_depth + 2;
56 } else {
57 0 $max_depth = $n;
58 }
59  
60 {
61 0 my $stretch_depth = $max_depth + 1;
62 0 my $stretch_tree = bottom_up_tree($stretch_depth);
63 # print "stretch tree of depth $stretch_depth\t check: ",
64 # item_check($stretch_tree), "\n";
65 }
66  
67 0 my $long_lived_tree = bottom_up_tree($max_depth);
68  
69 0 my $depth = $min_depth;
70 0 ▶ while ( $depth <= $max_depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= $max_depth) 0 0 0
71 0 my $iterations = 2 ** ($max_depth - $depth + $min_depth);
72 0 my $check = 0;
73  
74 0 foreach my $i (1..$iterations) {
75 0 my $temp_tree = bottom_up_tree($depth);
76 0 $check += item_check($temp_tree);
77  
78 0 $temp_tree = bottom_up_tree($depth);
79 0 $check += item_check($temp_tree);
80 }
81  
82 #print $iterations * 2, "\t trees of depth $depth\t check: ", $check, "\n";
83 0 $depth += 2;
84 }
85  
86 # print "long lived tree of depth $max_depth\t check: ",
87 # item_check($long_lived_tree), "\n";
88 }
89  
90 sub main
91 0 ▶{
Subroutine main: not called CC=3 SCAR=24.8
Pod undocumented
92 0 my ($options) = @_;
93  
94 0 ▶ my $goal = $options->{fastmode} ? 12 : 15;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'fastmode'} ? : 0 0 0
95 0 ▶ my $count = $options->{fastmode} ? 1 : 5;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'fastmode'} ? : 0 0 0
96  
97 0 my $result;
98 0 ▶ my $t = timeit $count, sub { $result = run($goal) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 return {
100 0 Benchmark => $t,
101 goal => $goal,
102 count => $count,
103 result => $result,
104 };
105 }
106  
107 1 1;
108  
109 __END__
« PerlStone2015/spectralnorm.pm ↑ Summary PerlStone2015/mandelbrot.pm »