Commit | Line | Data |
a0d0e21e |
1 | package Benchmark; |
2 | |
f06db76b |
3 | =head1 NAME |
4 | |
5 | Benchmark - benchmark running times of code |
6 | |
7 | timethis - run a chunk of code several times |
8 | |
9 | timethese - run several chunks of code several times |
10 | |
11 | timeit - run a chunk of code and see how long it goes |
12 | |
13 | =head1 SYNOPSIS |
14 | |
15 | timethis ($count, "code"); |
16 | |
523cc92b |
17 | # Use Perl code in strings... |
f06db76b |
18 | timethese($count, { |
19 | 'Name1' => '...code1...', |
20 | 'Name2' => '...code2...', |
21 | }); |
22 | |
523cc92b |
23 | # ... or use subroutine references. |
24 | timethese($count, { |
25 | 'Name1' => sub { ...code1... }, |
26 | 'Name2' => sub { ...code2... }, |
27 | }); |
28 | |
f06db76b |
29 | $t = timeit($count, '...other code...') |
30 | print "$count loops of other code took:",timestr($t),"\n"; |
31 | |
32 | =head1 DESCRIPTION |
33 | |
34 | The Benchmark module encapsulates a number of routines to help you |
35 | figure out how long it takes to execute some code. |
36 | |
37 | =head2 Methods |
38 | |
39 | =over 10 |
40 | |
41 | =item new |
42 | |
43 | Returns the current time. Example: |
44 | |
45 | use Benchmark; |
46 | $t0 = new Benchmark; |
47 | # ... your code here ... |
48 | $t1 = new Benchmark; |
49 | $td = timediff($t1, $t0); |
a24a9dfe |
50 | print "the code took:",timestr($td),"\n"; |
f06db76b |
51 | |
52 | =item debug |
53 | |
54 | Enables or disable debugging by setting the C<$Benchmark::Debug> flag: |
55 | |
523cc92b |
56 | debug Benchmark 1; |
f06db76b |
57 | $t = timeit(10, ' 5 ** $Global '); |
523cc92b |
58 | debug Benchmark 0; |
f06db76b |
59 | |
60 | =back |
61 | |
62 | =head2 Standard Exports |
63 | |
523cc92b |
64 | The following routines will be exported into your namespace |
f06db76b |
65 | if you use the Benchmark module: |
66 | |
67 | =over 10 |
68 | |
69 | =item timeit(COUNT, CODE) |
70 | |
523cc92b |
71 | Arguments: COUNT is the number of times to run the loop, and CODE is |
72 | the code to run. CODE may be either a code reference or a string to |
73 | be eval'd; either way it will be run in the caller's package. |
74 | |
75 | Returns: a Benchmark object. |
76 | |
77 | =item timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] ) |
78 | |
79 | Time COUNT iterations of CODE. CODE may be a string to eval or a |
80 | code reference; either way the CODE will run in the caller's package. |
81 | Results will be printed to STDOUT as TITLE followed by the times. |
82 | TITLE defaults to "timethis COUNT" if none is provided. STYLE |
83 | determines the format of the output, as described for timestr() below. |
84 | |
85 | =item timethese ( COUNT, CODEHASHREF, [ STYLE ] ) |
f06db76b |
86 | |
523cc92b |
87 | The CODEHASHREF is a reference to a hash containing names as keys |
88 | and either a string to eval or a code reference for each value. |
89 | For each (KEY, VALUE) pair in the CODEHASHREF, this routine will |
90 | call |
f06db76b |
91 | |
523cc92b |
92 | timethis(COUNT, VALUE, KEY, STYLE) |
f06db76b |
93 | |
523cc92b |
94 | =item timediff ( T1, T2 ) |
f06db76b |
95 | |
523cc92b |
96 | Returns the difference between two Benchmark times as a Benchmark |
97 | object suitable for passing to timestr(). |
f06db76b |
98 | |
523cc92b |
99 | =item timestr ( TIMEDIFF, [ STYLE, [ FORMAT ]] ) |
f06db76b |
100 | |
523cc92b |
101 | Returns a string that formats the times in the TIMEDIFF object in |
102 | the requested STYLE. TIMEDIFF is expected to be a Benchmark object |
103 | similar to that returned by timediff(). |
104 | |
105 | STYLE can be any of 'all', 'noc', 'nop' or 'auto'. 'all' shows each |
106 | of the 5 times available ('wallclock' time, user time, system time, |
107 | user time of children, and system time of children). 'noc' shows all |
108 | except the two children times. 'nop' shows only wallclock and the |
109 | two children times. 'auto' (the default) will act as 'all' unless |
110 | the children times are both zero, in which case it acts as 'noc'. |
111 | |
112 | FORMAT is the L<printf(3)>-style format specifier (without the |
113 | leading '%') to use to print the times. It defaults to '5.2f'. |
f06db76b |
114 | |
115 | =back |
116 | |
117 | =head2 Optional Exports |
118 | |
119 | The following routines will be exported into your namespace |
120 | if you specifically ask that they be imported: |
121 | |
122 | =over 10 |
123 | |
523cc92b |
124 | =item clearcache ( COUNT ) |
125 | |
126 | Clear the cached time for COUNT rounds of the null loop. |
127 | |
128 | =item clearallcache ( ) |
f06db76b |
129 | |
523cc92b |
130 | Clear all cached times. |
f06db76b |
131 | |
523cc92b |
132 | =item disablecache ( ) |
f06db76b |
133 | |
523cc92b |
134 | Disable caching of timings for the null loop. This will force Benchmark |
135 | to recalculate these timings for each new piece of code timed. |
136 | |
137 | =item enablecache ( ) |
138 | |
139 | Enable caching of timings for the null loop. The time taken for COUNT |
140 | rounds of the null loop will be calculated only once for each |
141 | different COUNT used. |
f06db76b |
142 | |
143 | =back |
144 | |
145 | =head1 NOTES |
146 | |
147 | The data is stored as a list of values from the time and times |
523cc92b |
148 | functions: |
f06db76b |
149 | |
150 | ($real, $user, $system, $children_user, $children_system) |
151 | |
152 | in seconds for the whole loop (not divided by the number of rounds). |
153 | |
154 | The timing is done using time(3) and times(3). |
155 | |
156 | Code is executed in the caller's package. |
157 | |
f06db76b |
158 | The time of the null loop (a loop with the same |
159 | number of rounds but empty loop body) is subtracted |
160 | from the time of the real loop. |
161 | |
162 | The null loop times are cached, the key being the |
163 | number of rounds. The caching can be controlled using |
164 | calls like these: |
165 | |
523cc92b |
166 | clearcache($key); |
f06db76b |
167 | clearallcache(); |
168 | |
523cc92b |
169 | disablecache(); |
f06db76b |
170 | enablecache(); |
171 | |
172 | =head1 INHERITANCE |
173 | |
174 | Benchmark inherits from no other class, except of course |
175 | for Exporter. |
176 | |
177 | =head1 CAVEATS |
178 | |
179 | The real time timing is done using time(2) and |
180 | the granularity is therefore only one second. |
181 | |
182 | Short tests may produce negative figures because perl |
523cc92b |
183 | can appear to take longer to execute the empty loop |
184 | than a short test; try: |
f06db76b |
185 | |
186 | timethis(100,'1'); |
187 | |
188 | The system time of the null loop might be slightly |
189 | more than the system time of the loop with the actual |
a24a9dfe |
190 | code and therefore the difference might end up being E<lt> 0. |
f06db76b |
191 | |
f06db76b |
192 | =head1 AUTHORS |
193 | |
a24a9dfe |
194 | Jarkko Hietaniemi E<lt>F<Jarkko.Hietaniemi@hut.fi>E<gt>, |
195 | Tim Bunce E<lt>F<Tim.Bunce@ig.co.uk>E<gt> |
f06db76b |
196 | |
197 | =head1 MODIFICATION HISTORY |
198 | |
199 | September 8th, 1994; by Tim Bunce. |
200 | |
523cc92b |
201 | March 28th, 1997; by Hugo van der Sanden: added support for code |
202 | references and the already documented 'debug' method; revamped |
203 | documentation. |
f06db76b |
204 | |
523cc92b |
205 | =cut |
a0d0e21e |
206 | |
4aa0a1f7 |
207 | use Carp; |
a0d0e21e |
208 | use Exporter; |
209 | @ISA=(Exporter); |
210 | @EXPORT=qw(timeit timethis timethese timediff timestr); |
211 | @EXPORT_OK=qw(clearcache clearallcache disablecache enablecache); |
212 | |
213 | &init; |
214 | |
215 | sub init { |
216 | $debug = 0; |
217 | $min_count = 4; |
218 | $min_cpu = 0.4; |
219 | $defaultfmt = '5.2f'; |
220 | $defaultstyle = 'auto'; |
221 | # The cache can cause a slight loss of sys time accuracy. If a |
222 | # user does many tests (>10) with *very* large counts (>10000) |
223 | # or works on a very slow machine the cache may be useful. |
224 | &disablecache; |
225 | &clearallcache; |
226 | } |
227 | |
523cc92b |
228 | sub debug { $debug = ($_[1] != 0); } |
229 | |
a0d0e21e |
230 | sub clearcache { delete $cache{$_[0]}; } |
231 | sub clearallcache { %cache = (); } |
232 | sub enablecache { $cache = 1; } |
233 | sub disablecache { $cache = 0; } |
234 | |
a0d0e21e |
235 | # --- Functions to process the 'time' data type |
236 | |
523cc92b |
237 | sub new { my @t = (time, times); print "new=@t\n" if $debug; bless \@t; } |
a0d0e21e |
238 | |
239 | sub cpu_p { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps ; } |
240 | sub cpu_c { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $cu+$cs ; } |
241 | sub cpu_a { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps+$cu+$cs ; } |
242 | sub real { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $r ; } |
243 | |
523cc92b |
244 | sub timediff { |
a0d0e21e |
245 | my($a, $b) = @_; |
523cc92b |
246 | my @r; |
247 | for ($i=0; $i < @$a; ++$i) { |
a0d0e21e |
248 | push(@r, $a->[$i] - $b->[$i]); |
249 | } |
250 | bless \@r; |
251 | } |
252 | |
523cc92b |
253 | sub timestr { |
a0d0e21e |
254 | my($tr, $style, $f) = @_; |
523cc92b |
255 | my @t = @$tr; |
a0d0e21e |
256 | warn "bad time value" unless @t==5; |
257 | my($r, $pu, $ps, $cu, $cs) = @t; |
258 | my($pt, $ct, $t) = ($tr->cpu_p, $tr->cpu_c, $tr->cpu_a); |
523cc92b |
259 | $f = $defaultfmt unless defined $f; |
a0d0e21e |
260 | # format a time in the required style, other formats may be added here |
523cc92b |
261 | $style = $defaultstyle unless defined $style; |
262 | $style = ($ct>0) ? 'all' : 'noc' if $style eq 'auto'; |
263 | my $s = "@t $style"; # default for unknown style |
a0d0e21e |
264 | $s=sprintf("%2d secs (%$f usr %$f sys + %$f cusr %$f csys = %$f cpu)", |
523cc92b |
265 | @t,$t) if $style eq 'all'; |
a0d0e21e |
266 | $s=sprintf("%2d secs (%$f usr %$f sys = %$f cpu)", |
523cc92b |
267 | $r,$pu,$ps,$pt) if $style eq 'noc'; |
a0d0e21e |
268 | $s=sprintf("%2d secs (%$f cusr %$f csys = %$f cpu)", |
523cc92b |
269 | $r,$cu,$cs,$ct) if $style eq 'nop'; |
a0d0e21e |
270 | $s; |
271 | } |
523cc92b |
272 | |
273 | sub timedebug { |
a0d0e21e |
274 | my($msg, $t) = @_; |
523cc92b |
275 | print STDERR "$msg",timestr($t),"\n" if $debug; |
a0d0e21e |
276 | } |
277 | |
a0d0e21e |
278 | # --- Functions implementing low-level support for timing loops |
279 | |
280 | sub runloop { |
281 | my($n, $c) = @_; |
4aa0a1f7 |
282 | |
283 | $n+=0; # force numeric now, so garbage won't creep into the eval |
523cc92b |
284 | croak "negative loopcount $n" if $n<0; |
285 | confess "Usage: runloop(number, [string | coderef])" unless defined $c; |
a0d0e21e |
286 | my($t0, $t1, $td); # before, after, difference |
287 | |
288 | # find package of caller so we can execute code there |
523cc92b |
289 | my($curpack) = caller(0); |
290 | my($i, $pack)= 0; |
a0d0e21e |
291 | while (($pack) = caller(++$i)) { |
292 | last if $pack ne $curpack; |
293 | } |
294 | |
0d72c55d |
295 | my $subcode = (ref $c eq 'CODE') |
296 | ? "sub { package $pack; my(\$_i)=$n; while (\$_i--){&\$c;} }" |
297 | : "sub { package $pack; my(\$_i)=$n; while (\$_i--){$c;} }"; |
a0d0e21e |
298 | my $subref = eval $subcode; |
4aa0a1f7 |
299 | croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@; |
523cc92b |
300 | print STDERR "runloop $n '$subcode'\n" if $debug; |
a0d0e21e |
301 | |
302 | $t0 = &new; |
303 | &$subref; |
304 | $t1 = &new; |
305 | $td = &timediff($t1, $t0); |
306 | |
307 | timedebug("runloop:",$td); |
308 | $td; |
309 | } |
310 | |
311 | |
312 | sub timeit { |
313 | my($n, $code) = @_; |
314 | my($wn, $wc, $wd); |
315 | |
316 | printf STDERR "timeit $n $code\n" if $debug; |
317 | |
523cc92b |
318 | if ($cache && exists $cache{$n}) { |
a0d0e21e |
319 | $wn = $cache{$n}; |
523cc92b |
320 | } else { |
a0d0e21e |
321 | $wn = &runloop($n, ''); |
322 | $cache{$n} = $wn; |
323 | } |
324 | |
325 | $wc = &runloop($n, $code); |
326 | |
327 | $wd = timediff($wc, $wn); |
328 | |
329 | timedebug("timeit: ",$wc); |
330 | timedebug(" - ",$wn); |
331 | timedebug(" = ",$wd); |
332 | |
333 | $wd; |
334 | } |
335 | |
a0d0e21e |
336 | # --- Functions implementing high-level time-then-print utilities |
337 | |
338 | sub timethis{ |
339 | my($n, $code, $title, $style) = @_; |
523cc92b |
340 | my $t = timeit($n, $code); |
341 | local $| = 1; |
342 | $title = "timethis $n" unless defined $title; |
343 | $style = "" unless defined $style; |
a0d0e21e |
344 | printf("%10s: ", $title); |
345 | print timestr($t, $style),"\n"; |
523cc92b |
346 | |
a0d0e21e |
347 | # A conservative warning to spot very silly tests. |
348 | # Don't assume that your benchmark is ok simply because |
349 | # you don't get this warning! |
350 | print " (warning: too few iterations for a reliable count)\n" |
523cc92b |
351 | if $n < $min_count |
a0d0e21e |
352 | || ($t->real < 1 && $n < 1000) |
523cc92b |
353 | || $t->cpu_a < $min_cpu; |
a0d0e21e |
354 | $t; |
355 | } |
356 | |
a0d0e21e |
357 | sub timethese{ |
358 | my($n, $alt, $style) = @_; |
359 | die "usage: timethese(count, { 'Name1'=>'code1', ... }\n" |
360 | unless ref $alt eq HASH; |
523cc92b |
361 | my @names = sort keys %$alt; |
362 | $style = "" unless defined $style; |
a0d0e21e |
363 | print "Benchmark: timing $n iterations of ",join(', ',@names),"...\n"; |
523cc92b |
364 | |
365 | # we could save the results in an array and produce a summary here |
a0d0e21e |
366 | # sum, min, max, avg etc etc |
523cc92b |
367 | map timethis($n, $alt->{$_}, $_, $style), @names; |
a0d0e21e |
368 | } |
369 | |
a0d0e21e |
370 | 1; |