line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Timer::CPU; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.100'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
29212
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8885
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require XSLoader; |
9
|
|
|
|
|
|
|
XSLoader::load('Timer::CPU', $VERSION); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub measure { |
13
|
1
|
|
|
1
|
0
|
14
|
my ($callback, %args) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
## Params |
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
6
|
die "measure needs code ref" unless ref $callback eq 'CODE'; |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
2
|
my $warm_ups = $args{warm_ups}; |
20
|
1
|
50
|
|
|
|
3
|
$warm_ups = 2 if !defined $warm_ups; |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
50
|
|
|
8
|
my $iterations = $args{iterations} || 1000; |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
50
|
|
|
7
|
my $method = $args{method} || 'min'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
## Vars |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
115
|
my @vals = (0) x $iterations; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## Warm-up the callback |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
|
|
5
|
for (my $i = 0; $i < $warm_ups; $i++) { |
35
|
2
|
|
|
|
|
13
|
measure_XS($callback); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
## Gather real data |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
40
|
for (my $i = 0; $i < $iterations; $i++) { |
42
|
1000
|
|
|
|
|
3933
|
$vals[$i] = measure_XS($callback); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
## Output |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
21
|
@vals = sort { $a <=> $b } @vals; |
|
8177
|
|
|
|
|
6774
|
|
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
|
|
8
|
if ($method eq 'min') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
51
|
1
|
|
|
|
|
38
|
return $vals[0]; |
52
|
|
|
|
|
|
|
} elsif ($method eq 'max') { |
53
|
0
|
|
|
|
|
|
return $vals[-1]; |
54
|
|
|
|
|
|
|
} elsif ($method eq 'median') { |
55
|
0
|
|
|
|
|
|
return $vals[int($iterations / 2)]; |
56
|
|
|
|
|
|
|
} elsif ($method eq 'mean') { |
57
|
0
|
|
|
|
|
|
my $total = 0; |
58
|
0
|
|
|
|
|
|
$total += $_ foreach (@vals); |
59
|
0
|
|
|
|
|
|
return $total / $iterations; |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
|
die "unknown method '$method'"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |