PerlBench.pm

filter: Statementstmt 32.6% 45 / 138 Branchbran 33.3% 22 / 66 Conditioncond 7.6% 1 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 5 totaltotal 31.2% 75 / 240 CC 48 SCAR 61.9
CC
48
Cov
31%
timeit
930.0
make_timeit_sub_code
30.0
sec_f
14.0
CRAP 487.3
↑ Summary PerlBench/Results.pm »
1 package PerlBench;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our @EXPORT_OK = qw(timeit make_timeit_sub_code sec_f);
6  
7 1 our $VERSION = "0.93";
8  
9 1 ▶use PerlBench::Stats qw(calc_stats);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 0 ▶sub timeit {
Subroutine timeit: not called CC=30 SCAR=68.4
Pod undocumented
15 0 my($code, %opt) = @_;
16 0 my $init = $opt{init};
17  
18 # XXX auto determine how long we need to time stuff
19 0 ▶ my $enough = $opt{enough} || 0.5;
Condition0%0/2
$opt{'enough'} || 0.5
l!l
00
MC/DC0%0/1
$opt{'enough'} || 0.5
$opt{'enough'}
Truth table0%0/2
A
A= $opt{'enough'}
Aresult
11
00
20  
21 # auto determine $loop_count and $repeat_count
22 print STDERR "# Determine loop count - enough is " . sec_f($enough) . "\n"
23 0 ▶ if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 0 0
24 0 my($loop_count, $repeat_count) = do {
25 0 my $count = 1;
26 0 ▶ my $repeat = $opt{repeat} || 1;
Condition0%0/2
$opt{'repeat'} || 1
l!l
00
MC/DC0%0/1
$opt{'repeat'} || 1
$opt{'repeat'}
Truth table0%0/2
A
A= $opt{'repeat'}
Aresult
11
00
27 0 while (1) {
28 0 ▶ print STDERR "# $count ==> " if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 0 0
29 0 my $t = timeit_once($code, $init, $count, $repeat);
30 0 ▶ print STDERR sec_f($t, undef), "\n" if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 0 0
31 0 ▶ last if $t > $enough;
Branch: 1 branch
BranchTrueFalseTotal
if $t > $enough 0 0 0
32 0 ▶ if ($t < 0.00001) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t < 1e-05) { } 0 0 0
elsif ($t < 0.01) { } 0 0 0
33 0 $count *= 1000;
34 0 next;
35 }
36 elsif ($t < 0.01) {
37 0 $count *= 2;
38 0 next;
39 }
40 0 $count = int($count * ($enough / $t) * 1.05) + 1;
41 }
42 0 ($count, $repeat);
43 };
44  
45 0 my @experiment;
46 0 push(@experiment, {
47 loop_count => $loop_count,
48 repeat_count => $repeat_count,
49 });
50 0 ▶ $loop_count++ if $loop_count % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $loop_count % 2 0 0 0
51 0 push(@experiment, {
52 loop_count => $loop_count / 2,
53 repeat_count => $repeat_count * 2,
54 });
55  
56 0 my $pl = "tt$$.pl";
57 0 ▶ open(my $fh, ">", $pl) || die "Can't create $pl: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $pl or die "Can't create ${pl}: $!" 0 0 0
58 0 print $fh "#!perl\n";
59 0 print $fh "use strict;\n";
60 0 print $fh "require Time::HiRes;\n";
61 0 ▶ print $fh "{\n $init;\n" if $init;
Branch: 1 branch
BranchTrueFalseTotal
if $init 0 0 0
62 0 print $fh "my \@TIMEIT = (\n";
63 0 for my $e (@experiment) {
64 0 print $fh &make_timeit_sub_code($code, undef, $e->{loop_count}, $e->{repeat_count}), ",\n";
65 }
66 0 print $fh ");\n";
67  
68 0 print $fh <<'EOT';
69  
70 my $e = shift || die;
71 my $trials = shift || die;
72 my $loop_count = shift || die;
73 my @t;
74 my $sum = 0;
75 for (1.. $trials) {
76 print "t$e=", $TIMEIT[$e-1]->(), "\n";
77 }
78 print "---\n";
79 EOT
80 0 ▶ print $fh "}\n" if $init;
Branch: 1 branch
BranchTrueFalseTotal
if $init 0 0 0
81 0 ▶ close($fh) || die "Can't write $pl: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't write ${pl}: $!" 0 0 0
82  
83 0 ▶ print STDERR "# Running tests...\n" if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 0 0
84 0 my $rounds = 4;
85 0 for my $round (1..$rounds) {
86 0 ▶ printf STDERR "# %.0f%%\n", (($round-1)/$rounds)*100 if $opt{verbose} && $round > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} and $round > 1 0 0 0
Condition0%0/3
$opt{"verbose"} and $round > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"verbose"} and $round > 1
$opt{"verbose"} $round > 1
Truth table0%0/3
A and B
A= $opt{"verbose"}B= $round > 1
ABresult
0X0
100
111
87 0 my $e_num = 0;
88 0 for my $e (@experiment) {
89 0 $e_num++;
90 0 ▶ open($fh, "$^X $pl $e_num 7 $loop_count |") || die "Can't run $pl: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "$^X $pl $e_num 7 $loop_count |" or die "Can't run ${pl}: $!" 0 0 0
91 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
92 #print "XXX $_";
93 0 ▶ if (/^t(\d+)=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^t(\d+)=(.*)/) 0 0 0
94 0 ▶ die unless $1 eq $e_num;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 eq $e_num 0 0 0
95 0 my $t = $2+0;
96 0 push(@{$e->{t}}, $t);
97 }
98 }
99 0 close($fh);
100 }
101 }
102 0 unlink($pl);
103 0 ▶ print STDERR "# done\n" if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 0 0
104  
105 0 for my $e (@experiment) {
106 0 ▶ my $t = $e->{t} ||return;
Condition0%0/2
$e->{'t'} || (return)
l!l
00
MC/DC0%0/1
$e->{'t'} || (return)
$e->{'t'}
Truth table0%0/2
A
A= $e->{'t'}
Aresult
11
00
107 0 calc_stats($e->{t}, $e);
108  
109 0 my $count = $e->{loop_count} * $e->{repeat_count};
110 0 $e->{count} = $count;
111 }
112  
113 0 my $loop_overhead = do {
114 0 my $e1 = $experiment[0];
115 0 my $e2 = $experiment[-1];
116 0 my $t1 = $e1->{med} / $e1->{loop_count};
117 0 my $t2 = $e2->{med} / $e2->{loop_count};
118 0 my $f = $e2->{repeat_count} / $e1->{repeat_count};
119 0 $f * $t1 - $t2;
120 };
121  
122 0 for my $e (@experiment) {
123 0 $e->{loop_overhead} = $loop_overhead * $e->{loop_count};
124 0 $e->{loop_overhead_relative} = $e->{loop_overhead} / $e->{med};
125 }
126  
127 0 my %res;
128 0 $res{x} = \@experiment;
129  
130 # calculate combined stats
131 0 my @t;
132 0 for my $e (@experiment) {
133 0 my $c = $e->{count};
134 0 my $o = $e->{loop_overhead};
135 0 push(@t, map { ($_-$o)/$c } @{$e->{t}});
136 }
137 0 calc_stats(\@t, \%res);
138  
139 0 for my $f (qw(count loop_overhead_relative)) {
140 # XXX avg
141 0 $res{$f} = $experiment[0]{$f};
142 }
143  
144 0 return \%res;
145 }
146  
147 0 ▶sub timeit_once {
Subroutine timeit_once: not called CC=1 SCAR=6.9
Pod undocumented
148 0 return make_timeit_sub(@_)->();
149 }
150  
151 0 ▶sub make_timeit_sub {
Subroutine make_timeit_sub: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my $code = make_timeit_sub_code(@_);
153 0 my $sub = eval $code;
154 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
155 0 return $sub;
156 }
157  
158 0 ▶sub make_timeit_sub_code {
Subroutine make_timeit_sub_code: not called CC=5 SCAR=34.0
Pod undocumented
159 0 my($code, $init, $loop_count, $repeat_count) = @_;
160 0 $loop_count = int($loop_count);
161 0 ▶ die unless $loop_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $loop_count > 0 0 0 0
162 0 ▶ die if $loop_count + 1 == $loop_count; # too large
Branch: 1 branch
BranchTrueFalseTotal
if $loop_count + 1 == $loop_count 0 0 0
163 0 ▶ $repeat_count ||= 1;
Condition0%0/2
$repeat_count ||= 1
l!l
00
MC/DC0%0/1
$repeat_count ||= 1
$repeat_count
Truth table0%0/2
A
A= $repeat_count
Aresult
11
00
164 0 ▶ $init = "" unless defined $init;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init 0 0 0
165 0 return <<EOT1 . "$init;$code" . <<'EOT2' . ($code x $repeat_count) . <<'EOT3';
166 sub {
167 my \$COUNT = $loop_count;
168 \$COUNT++;
169 package main;
170 EOT1
171  
172 my($BEFORE_S, $BEFORE_US) = Time::HiRes::gettimeofday();
173 while (--$COUNT) {
174 EOT2
175  
176 }
177 my($AFTER_S, $AFTER_US) = Time::HiRes::gettimeofday();
178 return ($AFTER_S - $BEFORE_S) + ($AFTER_US - $BEFORE_US)/1e6;
179 }
180 EOT3
181 }
182  
183 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
184 1 my %UNITS = (
185 "h" => 1/3600,
186 "min" => 1/60,
187 "s" => 1,
188 "ms" => 1e3,
189 "µs" => 1e6,
190 "ns" => 1e9,
191 );
192  
193 my @UNITS =
194 8 sort { $b->[1] <=> $a->[1] }
195 1 map { [$_ => $UNITS{$_}] }
196 keys %UNITS;
197  
198 20 ▶ sub sec_f {
Subroutine sec_f: called CC=14 SCAR=26.4
Pod undocumented
199 20 my($t, $d, $u) = @_;
200 20 my $f;
201 20 ▶ if (defined $u) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $u) { } 3 17 20
202 3 ▶ $f = $UNITS{$u} || croak("Unknown unit '$u'");
Condition50%1/2
$UNITS{$u} || croak("Unknown unit '${u}'")
l!l
30
MC/DC0%0/1
$UNITS{$u} || croak("Unknown unit '${u}'")
$UNITS{$u}
Truth table50%1/2
A
A= $UNITS{$u}
Aresult
11
00
203 }
204 else {
205 17 ▶ for (my $i = 1; $i < @UNITS; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @UNITS) 49 1 50
206 49 ▶ if ($t < 1/$UNITS[$i][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t < 1 / $UNITS[$i][1]) 16 33 49
207 16 ($u, $f) = @{$UNITS[$i-1]};
208 16 last;
209 }
210 }
211 17 ▶ unless ($u) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u) 1 16 17
212 1 ($u, $f) = @{$UNITS[-1]};
213 }
214 }
215  
216 20 ▶ my $dev = defined($d) ? 1 : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 4 16 20
217 20 ▶ $d = $t unless $dev;
Branch: 1 branch
BranchTrueFalseTotal
unless $dev 16 4 20
218  
219 20 ▶ if ($f != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f != 1) 13 7 20
220 13 $_ *= $f for $t, $d;
221 }
222  
223 20 my $p = 0;
224 20 ▶ if ($d < 0.05) {
Branch: 3 branches
BranchTrueFalseTotal
if ($d < 0.05) { } 2 18 20
elsif ($d < 0.5) { } 2 16 18
elsif ($d < 5) { } 7 9 16
225 2 $p = 3;
226 }
227 elsif ($d < 0.5) {
228 2 $p = 2;
229 }
230 elsif ($d < 5) {
231 7 $p = 1;
232 }
233  
234 20 ▶ $dev = sprintf(" ±%.*f", $p, $d) if $dev;
Branch: 1 branch
BranchTrueFalseTotal
if $dev 4 16 20
235 20 return sprintf("%.*f %s%s", $p, $t, $u, $dev);
236 }
237 }
238  
239 1 1;
↑ Summary PerlBench/Results.pm »