PerlStone2015/fannkuch.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 100 CC 9 SCAR 22.8
CC
9
Cov
0%
fannkuch
20.0
print30
12.0
main
12.0
CRAP 9.8
1 package Benchmark::Perl::Formance::Plugin::PerlStone2015::fannkuch;
2 our $AUTHORITY = 'cpan:SCHWIGON';
3 # ABSTRACT: benchmark - Indexed-access to tiny integer-sequence
4 $Benchmark::Perl::Formance::Plugin::PerlStone2015::fannkuch::VERSION = '0.002';
5 # COMMAND LINE:
6 # /usr/bin/perl fannkuch.perl-3.perl 12
7  
8 # The Computer Language Benchmarks Game
9 # http://shootout.alioth.debian.org/
10  
11 # Initial port from C by Steve Clark
12 # Rewrite by Kalev Soikonen
13 # Modified by Kuang-che Wu
14 # Multi-threaded by Andrew Rodland
15 # Benchmark::Perl::Formance plugin by Steffen Schwigon
16  
17 use strict;
18 use warnings;
19 no warnings 'uninitialized';
20  
21 #############################################################
22 # #
23 # Benchmark Code ahead - Don't touch without strong reason! #
24 # #
25 #############################################################
26  
27 use Benchmark ':hireswallclock';
28  
29 use integer;
30 use threads;
31  
32 sub fannkuch {
33 my ($n, $last) = @_;
34 my ($iter, $flips, $maxflips);
35 my (@q, @p, @count);
36  
37 @p = (1 .. $last - 1, $last + 1 .. $n, $last);
38 @count = (1..$n);
39  
40 TRY: while (1) {
41 if ($p[0] != 1 && $p[-1] != $n) {
42 @q = @p;
43 for ($flips=0; $q[0] != 1; $flips++) {
44 unshift @q, reverse splice @q, 0, $q[0];
45 }
46 $maxflips = $flips if $flips > $maxflips;
47 }
48  
49 for my $i (1 .. $n - 2) {
50 splice @p, $i, 0, shift @p;
51 next TRY if (--$count[$i]);
52 $count[$i] = $i + 1;
53 }
54 return $maxflips;
55 }
56 }
57  
58 sub print30 {
59 my ($n, $iter) = @_;
60 my @p = my @count = (1..$n);
61  
62 TRY: while (1) {
63 #print @p, "\n";
64 return if ++$iter >= 30;
65 for my $i (1 .. $n - 1) {
66 splice @p, $i, 0, shift @p;
67 next TRY if (--$count[$i]);
68 $count[$i] = $i + 1;
69 }
70 }
71 }
72  
73 sub run
74 {
75 my ($n) = @_;
76  
77 print30($n);
78  
79 my @threads;
80 for my $i (1 .. $n) {
81 push @threads, threads->create(\&fannkuch, $n, $i);
82 }
83  
84 my $max = 0;
85 for my $thread (@threads) {
86 my $val = $thread->join;
87 $max = $val if $val > $max;
88 }
89 return $max;
90 }
91  
92 sub main
93 {
94 my ($options) = @_;
95  
96 my $goal = $options->{fastmode} ? 9 : 10;
97 my $count = $options->{fastmode} ? 2 : 5;
98  
99 my $result;
100 my $t = timeit $count, sub { $result = run($goal) };
101 return {
102 Benchmark => $t,
103 goal => $goal,
104 count => $count,
105 result => $result,
106 };
107 }
108  
109 1;
110  
111 __END__