| 1 | package Benchmark::Perl::Formance::Plugin::Shootout::fannkuch; | ||
| 2 | our $AUTHORITY = 'cpan:SCHWIGON'; | ||
| 3 | # ABSTRACT: benchmark - Indexed-access to tiny integer-sequence | ||
| 4 | |||
| 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 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | no warnings 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | |||
| 21 | our $VERSION = "0.002"; | ||
| 22 | |||
| 23 | ############################################################# | ||
| 24 | # # | ||
| 25 | # Benchmark Code ahead - Don't touch without strong reason! # | ||
| 26 | # # | ||
| 27 | ############################################################# | ||
| 28 | |||
| 29 | 1 | ▶ | use Benchmark ':hireswallclock'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | 1 | ▶ | use integer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 33 | |||
| 34 | sub fannkuch { | ||
| 35 | my ($n, $last) = @_; | ||
| 36 | my ($iter, $flips, $maxflips); | ||
| 37 | my (@q, @p, @count); | ||
| 38 | |||
| 39 | @p = (1 .. $last - 1, $last + 1 .. $n, $last); | ||
| 40 | @count = (1..$n); | ||
| 41 | |||
| 42 | TRY: while (1) { | ||
| 43 | if ($p[0] != 1 && $p[-1] != $n) { | ||
| 44 | @q = @p; | ||
| 45 | for ($flips=0; $q[0] != 1; $flips++) { | ||
| 46 | unshift @q, reverse splice @q, 0, $q[0]; | ||
| 47 | } | ||
| 48 | $maxflips = $flips if $flips > $maxflips; | ||
| 49 | } | ||
| 50 | |||
| 51 | for my $i (1 .. $n - 2) { | ||
| 52 | splice @p, $i, 0, shift @p; | ||
| 53 | next TRY if (--$count[$i]); | ||
| 54 | $count[$i] = $i + 1; | ||
| 55 | } | ||
| 56 | return $maxflips; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub print30 { | ||
| 61 | my ($n, $iter) = @_; | ||
| 62 | my @p = my @count = (1..$n); | ||
| 63 | |||
| 64 | TRY: while (1) { | ||
| 65 | #print @p, "\n"; | ||
| 66 | return if ++$iter >= 30; | ||
| 67 | for my $i (1 .. $n - 1) { | ||
| 68 | splice @p, $i, 0, shift @p; | ||
| 69 | next TRY if (--$count[$i]); | ||
| 70 | $count[$i] = $i + 1; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub run | ||
| 76 | { | ||
| 77 | my ($n) = @_; | ||
| 78 | |||
| 79 | print30($n); | ||
| 80 | |||
| 81 | my @threads; | ||
| 82 | for my $i (1 .. $n) { | ||
| 83 | push @threads, threads->create(\&fannkuch, $n, $i); | ||
| 84 | } | ||
| 85 | |||
| 86 | my $max = 0; | ||
| 87 | for my $thread (@threads) { | ||
| 88 | my $val = $thread->join; | ||
| 89 | $max = $val if $val > $max; | ||
| 90 | } | ||
| 91 | return $max; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub main | ||
| 95 | { | ||
| 96 | my ($options) = @_; | ||
| 97 | |||
| 98 | my $goal = $options->{fastmode} ? 8 : 10; | ||
| 99 | my $count = $options->{fastmode} ? 1 : 5; | ||
| 100 | |||
| 101 | my $result; | ||
| 102 | my $t = timeit $count, sub { $result = run($goal) }; | ||
| 103 | return { | ||
| 104 | Benchmark => $t, | ||
| 105 | goal => $goal, | ||
| 106 | count => $count, | ||
| 107 | result => $result, | ||
| 108 | }; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | __END__ | ||