« blib/lib/Benchmark/Perl/Formance/Plugin/FibOOSig.pm
↑ Summary
blib/lib/Benchmark/Perl/Formance/Plugin/Mem.pm »
| 1 | package Benchmark::Perl::Formance::Plugin::Shootout::binarytrees; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||
| 3 | # ABSTRACT: benchmark - Allocate and deallocate many many binary trees | ||||||||||
| 4 | |||||||||||
| 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 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use integer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use Benchmark ':hireswallclock'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 2 | our $VERSION = "0.002"; | |||||||||
| 22 | |||||||||||
| 23 | ############################################################# | ||||||||||
| 24 | # # | ||||||||||
| 25 | # Benchmark Code ahead - Don't touch without strong reason! # | ||||||||||
| 26 | # # | ||||||||||
| 27 | ############################################################# | ||||||||||
| 28 | |||||||||||
| 29 | 259424 | ▶ | sub item_check { | ||||||||
|
Subroutine
item_check: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 259424 | my ($tree) = @_; | |||||||||
| 31 | |||||||||||
| 32 | 259424 | ▶ | return $tree->[2] unless (defined $tree->[0]); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 128352 | return $tree->[2] + item_check($tree->[0]) - item_check($tree->[1]); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | 2722 | ▶ | sub bottom_up_tree { | ||||||||
|
Subroutine
bottom_up_tree: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 38 | 2722 | my($depth) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 2722 | my @pool; | |||||||||
| 41 | 2722 | push @pool, [undef, undef, -$_] foreach 0..2**$depth-1; | |||||||||
| 42 | |||||||||||
| 43 | 2722 | foreach my $exponent (reverse(0..($depth-1))) { | |||||||||
| 44 | push @pool, [reverse(splice(@pool, 0, 2)), $_] | ||||||||||
| 45 | 12629 | foreach reverse(-(2**$exponent-1) .. 0); | |||||||||
| 46 | } | ||||||||||
| 47 | 2722 | return $pool[0]; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 51 | 1 | my ($n) = @_; | |||||||||
| 52 | |||||||||||
| 53 | 1 | my $min_depth = 4; | |||||||||
| 54 | 1 | my $max_depth; | |||||||||
| 55 | |||||||||||
| 56 | 1 | ▶ | if ( ($min_depth + 2) > $n) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | $max_depth = $min_depth + 2; | |||||||||
| 58 | } else { | ||||||||||
| 59 | 1 | $max_depth = $n; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | { | ||||||||||
| 63 | 1 | my $stretch_depth = $max_depth + 1; | |||||||||
| 64 | 1 | my $stretch_tree = bottom_up_tree($stretch_depth); | |||||||||
| 65 | # print "stretch tree of depth $stretch_depth\t check: ", | ||||||||||
| 66 | # item_check($stretch_tree), "\n"; | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 1 | my $long_lived_tree = bottom_up_tree($max_depth); | |||||||||
| 70 | |||||||||||
| 71 | 1 | my $depth = $min_depth; | |||||||||
| 72 | 1 | ▶ | while ( $depth <= $max_depth ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 4 | my $iterations = 2 ** ($max_depth - $depth + $min_depth); | |||||||||
| 74 | 4 | my $check = 0; | |||||||||
| 75 | |||||||||||
| 76 | 4 | foreach my $i (1..$iterations) { | |||||||||
| 77 | 1360 | my $temp_tree = bottom_up_tree($depth); | |||||||||
| 78 | 1360 | $check += item_check($temp_tree); | |||||||||
| 79 | |||||||||||
| 80 | 1360 | $temp_tree = bottom_up_tree($depth); | |||||||||
| 81 | 1360 | $check += item_check($temp_tree); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | #print $iterations * 2, "\t trees of depth $depth\t check: ", $check, "\n"; | ||||||||||
| 85 | 4 | $depth += 2; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | # print "long lived tree of depth $max_depth\t check: ", | ||||||||||
| 89 | # item_check($long_lived_tree), "\n"; | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | sub main | ||||||||||
| 93 | 1 | ▶ | { | ||||||||
|
Subroutine
main: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 94 | 1 | my ($options) = @_; | |||||||||
| 95 | |||||||||||
| 96 | 1 | ▶ | my $goal = $options->{fastmode} ? 10 : 15; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 1 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | |||||||||||
| 99 | 1 | my $result; | |||||||||
| 100 | 1 | ▶ | my $t = timeit $count, sub { $result = run($goal) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 101 | return { | ||||||||||
| 102 | 1 | Benchmark => $t, | |||||||||
| 103 | goal => $goal, | ||||||||||
| 104 | count => $count, | ||||||||||
| 105 | result => $result, | ||||||||||
| 106 | }; | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 2 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||