PerlStone2015/nbody.pm

filter: Statementstmt 25.0% 22 / 88 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 5 totaltotal 25.2% 28 / 111 CC 9 SCAR 19.5
CC
9
Cov
23%
advance
12.0
energy
12.0
main
12.0
CRAP 7.0
« PerlStone2015/05regex.pm ↑ Summary PerlStone2015/02bits.pm »
1 package Benchmark::Perl::Formance::Plugin::PerlStone2015::nbody;
2 1 our $AUTHORITY = 'cpan:SCHWIGON';
3 # ABSTRACT: benchmark - Double-precision N-body simulation
4 1 $Benchmark::Perl::Formance::Plugin::PerlStone2015::nbody::VERSION = '0.002';
5 # The Computer Language Shootout
6 # http://shootout.alioth.debian.org/
7 #
8 # contributed by Christoph Bauer
9 # converted into Perl by Márton Papp
10 # fixed and cleaned up by Danny Sauer
11 # optimized by Jesse Millikan
12 # Benchmark::Perl::Formance plugin by Steffen Schwigon
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #############################################################
18 # #
19 # Benchmark Code ahead - Don't touch without strong reason! #
20 # #
21 #############################################################
22  
23 1 ▶use constant PI => 3.141592653589793;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant SOLAR_MASS => (4 * PI * PI);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant DAYS_PER_YEAR => 365.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Benchmark ':hireswallclock';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Globals for arrays... Oh well.
30 # Almost every iteration is a range, so I keep the last index rather than a count.
31 1 my (@xs, @ys, @zs, @vxs, @vys, @vzs, @mass, $last);
32  
33 sub advance($) ## no critic
34 0 ▶{
Subroutine advance: not called CC=3 SCAR=24.8
Pod undocumented
35 0 my ($dt) = @_;
36 0 my ($mm, $mm2, $j, $dx, $dy, $dz, $distance, $mag);
37  
38 # This is faster in the outer loop...
39 0 for (0..$last) {
40 # But not in the inner loop. Strange.
41 0 ▶ for ($j = $_ + 1; $j < $last + 1; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $last + 1) 0 0 0
42 0 $dx = $xs[$_] - $xs[$j];
43 0 $dy = $ys[$_] - $ys[$j];
44 0 $dz = $zs[$_] - $zs[$j];
45 0 $distance = sqrt($dx * $dx + $dy * $dy + $dz * $dz);
46 0 $mag = $dt / ($distance * $distance * $distance);
47 0 $mm = $mass[$_] * $mag;
48 0 $mm2 = $mass[$j] * $mag;
49 0 $vxs[$_] -= $dx * $mm2;
50 0 $vxs[$j] += $dx * $mm;
51 0 $vys[$_] -= $dy * $mm2;
52 0 $vys[$j] += $dy * $mm;
53 0 $vzs[$_] -= $dz * $mm2;
54 0 $vzs[$j] += $dz * $mm;
55 }
56  
57 # We're done with planet $_ at this point
58 # This could be done in a seperate loop, but it's slower
59 0 $xs[$_] += $dt * $vxs[$_];
60 0 $ys[$_] += $dt * $vys[$_];
61 0 $zs[$_] += $dt * $vzs[$_];
62 }
63 }
64  
65 sub energy
66 0 ▶{
Subroutine energy: not called CC=3 SCAR=24.8
Pod undocumented
67 0 my ($e, $dx, $dy, $dz, $distance);
68  
69 0 $e = 0.0;
70 0 for my $i (0..$last) {
71 0 $e += 0.5 * $mass[$i] *
72 ($vxs[$i] * $vxs[$i] + $vys[$i] * $vys[$i] + $vzs[$i] * $vzs[$i]);
73 0 for ($i + 1..$last) {
74 0 $dx = $xs[$i] - $xs[$_];
75 0 $dy = $ys[$i] - $ys[$_];
76 0 $dz = $zs[$i] - $zs[$_];
77 0 $distance = sqrt($dx * $dx + $dy * $dy + $dz * $dz);
78 0 $e -= ($mass[$i] * $mass[$_]) / $distance;
79 }
80 }
81 0 return $e;
82 }
83  
84 sub offset_momentum
85 0 ▶{
Subroutine offset_momentum: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my ($px, $py, $pz) = (0.0, 0.0, 0.0);
87  
88 0 for (0..$last) {
89 0 $px += $vxs[$_] * $mass[$_];
90 0 $py += $vys[$_] * $mass[$_];
91 0 $pz += $vzs[$_] * $mass[$_];
92 }
93 0 $vxs[0] = - $px / SOLAR_MASS;
94 0 $vys[0] = - $py / SOLAR_MASS;
95 0 $vzs[0] = - $pz / SOLAR_MASS;
96 }
97  
98 sub run
99 0 ▶{
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
100 0 my ($n) = @_;
101  
102 # @ns = ( sun, jupiter, saturn, uranus, neptune )
103 0 @xs = (0, 4.84143144246472090e+00, 8.34336671824457987e+00, 1.28943695621391310e+01, 1.53796971148509165e+01);
104 0 @ys = (0, -1.16032004402742839e+00, 4.12479856412430479e+00, -1.51111514016986312e+01, -2.59193146099879641e+01);
105 0 @zs = (0, -1.03622044471123109e-01, -4.03523417114321381e-01, -2.23307578892655734e-01, 1.79258772950371181e-01);
106 0 @vxs = map {$_ * DAYS_PER_YEAR}
107 (0, 1.66007664274403694e-03, -2.76742510726862411e-03, 2.96460137564761618e-03, 2.68067772490389322e-03);
108 0 @vys = map {$_ * DAYS_PER_YEAR}
109 (0, 7.69901118419740425e-03, 4.99852801234917238e-03, 2.37847173959480950e-03, 1.62824170038242295e-03);
110 0 @vzs = map {$_ * DAYS_PER_YEAR}
111 (0, -6.90460016972063023e-05, 2.30417297573763929e-05, -2.96589568540237556e-05, -9.51592254519715870e-05);
112 0 @mass = map {$_ * SOLAR_MASS}
113 (1, 9.54791938424326609e-04, 2.85885980666130812e-04, 4.36624404335156298e-05, 5.15138902046611451e-05);
114  
115 0 $last = @xs - 1;
116  
117 0 offset_momentum();
118 0 my $result = [];
119 0 push @$result, sprintf("%.9f", energy());
120  
121 # This does not, in fact, consume N*4 bytes of memory
122 0 for (1..$n) {
123 0 advance(0.01);
124 }
125  
126 0 push @$result, sprintf ("%.9f", energy());
127 0 return $result;
128 }
129  
130 sub main
131 0 ▶{
Subroutine main: not called CC=3 SCAR=24.8
Pod undocumented
132 0 my ($options) = @_;
133  
134 0 ▶ my $goal = $options->{fastmode} ? 100_000 : 400_000;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'fastmode'} ? : 0 0 0
135 0 ▶ my $count = $options->{fastmode} ? 1 : 5;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'fastmode'} ? : 0 0 0
136  
137 0 my $result;
138 0 ▶ my $t = timeit $count, sub { $result = run($goal) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 return {
140 0 Benchmark => $t,
141 goal => $goal,
142 count => $count,
143 result => $result,
144 };
145 }
146  
147 1 1;
148  
149 __END__
« PerlStone2015/05regex.pm ↑ Summary PerlStone2015/02bits.pm »