File Coverage

blib/lib/PerlBench/Stats.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 4 100.0
condition 1 2 50.0
subroutine 3 3 100.0
pod 0 1 0.0
total 32 34 94.1


line stmt bran cond sub pod time code
1             package PerlBench::Stats;
2              
3 1     1   3622 use strict;
  1         2  
  1         25  
4              
5 1     1   4 use base 'Exporter';
  1         2  
  1         226  
6             our @EXPORT_OK = qw(calc_stats);
7             our $VERSION = "1.001";
8              
9              
10             sub calc_stats {
11 6     6 0 1408 my($samples, $hash) = @_;
12 6   50     31 $hash ||= {};
13 6         15 my @t = sort {$a <=> $b} @$samples;
  8         15  
14 6         11 my $n = @t;
15 6 100       15 return undef unless $n;
16              
17 5         7 my $sum = 0;
18 5         7 my $sum2 = 0;
19 5         15 for (@t) {
20 12         16 $sum += $_;
21 12         19 $sum2 += $_ * $_;
22             }
23              
24 5         11 $hash->{avg} = $sum / $n;
25 5         12 $hash->{stddev} = sqrt(($sum2 - ($sum * $sum)/$n) / $n);
26              
27 5         8 $hash->{min} = $t[0];
28 5 100       17 $hash->{med} = ($n % 2) ? $t[$n/2] : (($t[$n/2-1] + $t[$n/2])/2);
29 5         6 $hash->{max} = $t[-1];
30 5         11 $hash->{n} = $n;
31              
32 5         10 return $hash;
33             }
34              
35             1;