| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Benchmark 3" |
| 132 | .TH Benchmark 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Benchmark \- benchmark running times of Perl code |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& use Benchmark qw(:all) ; |
| 139 | .Ve |
| 140 | .PP |
| 141 | .Vb 1 |
| 142 | \& timethis ($count, "code"); |
| 143 | .Ve |
| 144 | .PP |
| 145 | .Vb 5 |
| 146 | \& # Use Perl code in strings... |
| 147 | \& timethese($count, { |
| 148 | \& 'Name1' => '...code1...', |
| 149 | \& 'Name2' => '...code2...', |
| 150 | \& }); |
| 151 | .Ve |
| 152 | .PP |
| 153 | .Vb 5 |
| 154 | \& # ... or use subroutine references. |
| 155 | \& timethese($count, { |
| 156 | \& 'Name1' => sub { ...code1... }, |
| 157 | \& 'Name2' => sub { ...code2... }, |
| 158 | \& }); |
| 159 | .Ve |
| 160 | .PP |
| 161 | .Vb 5 |
| 162 | \& # cmpthese can be used both ways as well |
| 163 | \& cmpthese($count, { |
| 164 | \& 'Name1' => '...code1...', |
| 165 | \& 'Name2' => '...code2...', |
| 166 | \& }); |
| 167 | .Ve |
| 168 | .PP |
| 169 | .Vb 4 |
| 170 | \& cmpthese($count, { |
| 171 | \& 'Name1' => sub { ...code1... }, |
| 172 | \& 'Name2' => sub { ...code2... }, |
| 173 | \& }); |
| 174 | .Ve |
| 175 | .PP |
| 176 | .Vb 9 |
| 177 | \& # ...or in two stages |
| 178 | \& $results = timethese($count, |
| 179 | \& { |
| 180 | \& 'Name1' => sub { ...code1... }, |
| 181 | \& 'Name2' => sub { ...code2... }, |
| 182 | \& }, |
| 183 | \& 'none' |
| 184 | \& ); |
| 185 | \& cmpthese( $results ) ; |
| 186 | .Ve |
| 187 | .PP |
| 188 | .Vb 2 |
| 189 | \& $t = timeit($count, '...other code...') |
| 190 | \& print "$count loops of other code took:",timestr($t),"\en"; |
| 191 | .Ve |
| 192 | .PP |
| 193 | .Vb 3 |
| 194 | \& $t = countit($time, '...other code...') |
| 195 | \& $count = $t->iters ; |
| 196 | \& print "$count loops of other code took:",timestr($t),"\en"; |
| 197 | .Ve |
| 198 | .PP |
| 199 | .Vb 2 |
| 200 | \& # enable hires wallclock timing if possible |
| 201 | \& use Benchmark ':hireswallclock'; |
| 202 | .Ve |
| 203 | .SH "DESCRIPTION" |
| 204 | .IX Header "DESCRIPTION" |
| 205 | The Benchmark module encapsulates a number of routines to help you |
| 206 | figure out how long it takes to execute some code. |
| 207 | .PP |
| 208 | timethis \- run a chunk of code several times |
| 209 | .PP |
| 210 | timethese \- run several chunks of code several times |
| 211 | .PP |
| 212 | cmpthese \- print results of timethese as a comparison chart |
| 213 | .PP |
| 214 | timeit \- run a chunk of code and see how long it goes |
| 215 | .PP |
| 216 | countit \- see how many times a chunk of code runs in a given time |
| 217 | .Sh "Methods" |
| 218 | .IX Subsection "Methods" |
| 219 | .IP "new" 10 |
| 220 | .IX Item "new" |
| 221 | Returns the current time. Example: |
| 222 | .Sp |
| 223 | .Vb 6 |
| 224 | \& use Benchmark; |
| 225 | \& $t0 = new Benchmark; |
| 226 | \& # ... your code here ... |
| 227 | \& $t1 = new Benchmark; |
| 228 | \& $td = timediff($t1, $t0); |
| 229 | \& print "the code took:",timestr($td),"\en"; |
| 230 | .Ve |
| 231 | .IP "debug" 10 |
| 232 | .IX Item "debug" |
| 233 | Enables or disable debugging by setting the \f(CW$Benchmark::Debug\fR flag: |
| 234 | .Sp |
| 235 | .Vb 3 |
| 236 | \& debug Benchmark 1; |
| 237 | \& $t = timeit(10, ' 5 ** $Global '); |
| 238 | \& debug Benchmark 0; |
| 239 | .Ve |
| 240 | .IP "iters" 10 |
| 241 | .IX Item "iters" |
| 242 | Returns the number of iterations. |
| 243 | .Sh "Standard Exports" |
| 244 | .IX Subsection "Standard Exports" |
| 245 | The following routines will be exported into your namespace |
| 246 | if you use the Benchmark module: |
| 247 | .IP "timeit(\s-1COUNT\s0, \s-1CODE\s0)" 10 |
| 248 | .IX Item "timeit(COUNT, CODE)" |
| 249 | Arguments: \s-1COUNT\s0 is the number of times to run the loop, and \s-1CODE\s0 is |
| 250 | the code to run. \s-1CODE\s0 may be either a code reference or a string to |
| 251 | be eval'd; either way it will be run in the caller's package. |
| 252 | .Sp |
| 253 | Returns: a Benchmark object. |
| 254 | .IP "timethis ( \s-1COUNT\s0, \s-1CODE\s0, [ \s-1TITLE\s0, [ \s-1STYLE\s0 ]] )" 10 |
| 255 | .IX Item "timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] )" |
| 256 | Time \s-1COUNT\s0 iterations of \s-1CODE\s0. \s-1CODE\s0 may be a string to eval or a |
| 257 | code reference; either way the \s-1CODE\s0 will run in the caller's package. |
| 258 | Results will be printed to \s-1STDOUT\s0 as \s-1TITLE\s0 followed by the times. |
| 259 | \&\s-1TITLE\s0 defaults to \*(L"timethis \s-1COUNT\s0\*(R" if none is provided. \s-1STYLE\s0 |
| 260 | determines the format of the output, as described for \fItimestr()\fR below. |
| 261 | .Sp |
| 262 | The \s-1COUNT\s0 can be zero or negative: this means the \fIminimum number of |
| 263 | \&\s-1CPU\s0 seconds\fR to run. A zero signifies the default of 3 seconds. For |
| 264 | example to run at least for 10 seconds: |
| 265 | .Sp |
| 266 | .Vb 1 |
| 267 | \& timethis(-10, $code) |
| 268 | .Ve |
| 269 | .Sp |
| 270 | or to run two pieces of code tests for at least 3 seconds: |
| 271 | .Sp |
| 272 | .Vb 1 |
| 273 | \& timethese(0, { test1 => '...', test2 => '...'}) |
| 274 | .Ve |
| 275 | .Sp |
| 276 | \&\s-1CPU\s0 seconds is, in \s-1UNIX\s0 terms, the user time plus the system time of |
| 277 | the process itself, as opposed to the real (wallclock) time and the |
| 278 | time spent by the child processes. Less than 0.1 seconds is not |
| 279 | accepted (\-0.01 as the count, for example, will cause a fatal runtime |
| 280 | exception). |
| 281 | .Sp |
| 282 | Note that the \s-1CPU\s0 seconds is the \fBminimum\fR time: \s-1CPU\s0 scheduling and |
| 283 | other operating system factors may complicate the attempt so that a |
| 284 | little bit more time is spent. The benchmark output will, however, |
| 285 | also tell the number of \f(CW$code\fR runs/second, which should be a more |
| 286 | interesting number than the actually spent seconds. |
| 287 | .Sp |
| 288 | Returns a Benchmark object. |
| 289 | .IP "timethese ( \s-1COUNT\s0, \s-1CODEHASHREF\s0, [ \s-1STYLE\s0 ] )" 10 |
| 290 | .IX Item "timethese ( COUNT, CODEHASHREF, [ STYLE ] )" |
| 291 | The \s-1CODEHASHREF\s0 is a reference to a hash containing names as keys |
| 292 | and either a string to eval or a code reference for each value. |
| 293 | For each (\s-1KEY\s0, \s-1VALUE\s0) pair in the \s-1CODEHASHREF\s0, this routine will |
| 294 | call |
| 295 | .Sp |
| 296 | .Vb 1 |
| 297 | \& timethis(COUNT, VALUE, KEY, STYLE) |
| 298 | .Ve |
| 299 | .Sp |
| 300 | The routines are called in string comparison order of \s-1KEY\s0. |
| 301 | .Sp |
| 302 | The \s-1COUNT\s0 can be zero or negative, see \fItimethis()\fR. |
| 303 | .Sp |
| 304 | Returns a hash of Benchmark objects, keyed by name. |
| 305 | .IP "timediff ( T1, T2 )" 10 |
| 306 | .IX Item "timediff ( T1, T2 )" |
| 307 | Returns the difference between two Benchmark times as a Benchmark |
| 308 | object suitable for passing to \fItimestr()\fR. |
| 309 | .IP "timestr ( \s-1TIMEDIFF\s0, [ \s-1STYLE\s0, [ \s-1FORMAT\s0 ] ] )" 10 |
| 310 | .IX Item "timestr ( TIMEDIFF, [ STYLE, [ FORMAT ] ] )" |
| 311 | Returns a string that formats the times in the \s-1TIMEDIFF\s0 object in |
| 312 | the requested \s-1STYLE\s0. \s-1TIMEDIFF\s0 is expected to be a Benchmark object |
| 313 | similar to that returned by \fItimediff()\fR. |
| 314 | .Sp |
| 315 | \&\s-1STYLE\s0 can be any of 'all', 'none', 'noc', 'nop' or 'auto'. 'all' shows |
| 316 | each of the 5 times available ('wallclock' time, user time, system time, |
| 317 | user time of children, and system time of children). 'noc' shows all |
| 318 | except the two children times. 'nop' shows only wallclock and the |
| 319 | two children times. 'auto' (the default) will act as 'all' unless |
| 320 | the children times are both zero, in which case it acts as 'noc'. |
| 321 | \&'none' prevents output. |
| 322 | .Sp |
| 323 | \&\s-1FORMAT\s0 is the \fIprintf\fR\|(3)\-style format specifier (without the |
| 324 | leading '%') to use to print the times. It defaults to '5.2f'. |
| 325 | .Sh "Optional Exports" |
| 326 | .IX Subsection "Optional Exports" |
| 327 | The following routines will be exported into your namespace |
| 328 | if you specifically ask that they be imported: |
| 329 | .IP "clearcache ( \s-1COUNT\s0 )" 10 |
| 330 | .IX Item "clearcache ( COUNT )" |
| 331 | Clear the cached time for \s-1COUNT\s0 rounds of the null loop. |
| 332 | .IP "clearallcache ( )" 10 |
| 333 | .IX Item "clearallcache ( )" |
| 334 | Clear all cached times. |
| 335 | .IP "cmpthese ( \s-1COUNT\s0, \s-1CODEHASHREF\s0, [ \s-1STYLE\s0 ] )" 10 |
| 336 | .IX Item "cmpthese ( COUNT, CODEHASHREF, [ STYLE ] )" |
| 337 | .PD 0 |
| 338 | .IP "cmpthese ( \s-1RESULTSHASHREF\s0, [ \s-1STYLE\s0 ] )" 10 |
| 339 | .IX Item "cmpthese ( RESULTSHASHREF, [ STYLE ] )" |
| 340 | .PD |
| 341 | Optionally calls \fItimethese()\fR, then outputs comparison chart. This: |
| 342 | .Sp |
| 343 | .Vb 1 |
| 344 | \& cmpthese( -1, { a => "++\e$i", b => "\e$i *= 2" } ) ; |
| 345 | .Ve |
| 346 | .Sp |
| 347 | outputs a chart like: |
| 348 | .Sp |
| 349 | .Vb 3 |
| 350 | \& Rate b a |
| 351 | \& b 2831802/s -- -61% |
| 352 | \& a 7208959/s 155% -- |
| 353 | .Ve |
| 354 | .Sp |
| 355 | This chart is sorted from slowest to fastest, and shows the percent speed |
| 356 | difference between each pair of tests. |
| 357 | .Sp |
| 358 | c<cmpthese> can also be passed the data structure that \fItimethese()\fR returns: |
| 359 | .Sp |
| 360 | .Vb 2 |
| 361 | \& $results = timethese( -1, { a => "++\e$i", b => "\e$i *= 2" } ) ; |
| 362 | \& cmpthese( $results ); |
| 363 | .Ve |
| 364 | .Sp |
| 365 | in case you want to see both sets of results. |
| 366 | .Sp |
| 367 | Returns a reference to an \s-1ARRAY\s0 of rows, each row is an \s-1ARRAY\s0 of cells from the |
| 368 | above chart, including labels. This: |
| 369 | .Sp |
| 370 | .Vb 1 |
| 371 | \& my $rows = cmpthese( -1, { a => '++$i', b => '$i *= 2' }, "none" ); |
| 372 | .Ve |
| 373 | .Sp |
| 374 | returns a data structure like: |
| 375 | .Sp |
| 376 | .Vb 5 |
| 377 | \& [ |
| 378 | \& [ '', 'Rate', 'b', 'a' ], |
| 379 | \& [ 'b', '2885232/s', '--', '-59%' ], |
| 380 | \& [ 'a', '7099126/s', '146%', '--' ], |
| 381 | \& ] |
| 382 | .Ve |
| 383 | .Sp |
| 384 | \&\fB\s-1NOTE\s0\fR: This result value differs from previous versions, which returned |
| 385 | the \f(CW\*(C`timethese()\*(C'\fR result structure. If you want that, just use the two |
| 386 | statement \f(CW\*(C`timethese\*(C'\fR...\f(CW\*(C`cmpthese\*(C'\fR idiom shown above. |
| 387 | .Sp |
| 388 | Incidently, note the variance in the result values between the two examples; |
| 389 | this is typical of benchmarking. If this were a real benchmark, you would |
| 390 | probably want to run a lot more iterations. |
| 391 | .IP "countit(\s-1TIME\s0, \s-1CODE\s0)" 10 |
| 392 | .IX Item "countit(TIME, CODE)" |
| 393 | Arguments: \s-1TIME\s0 is the minimum length of time to run \s-1CODE\s0 for, and \s-1CODE\s0 is |
| 394 | the code to run. \s-1CODE\s0 may be either a code reference or a string to |
| 395 | be eval'd; either way it will be run in the caller's package. |
| 396 | .Sp |
| 397 | \&\s-1TIME\s0 is \fInot\fR negative. \fIcountit()\fR will run the loop many times to |
| 398 | calculate the speed of \s-1CODE\s0 before running it for \s-1TIME\s0. The actual |
| 399 | time run for will usually be greater than \s-1TIME\s0 due to system clock |
| 400 | resolution, so it's best to look at the number of iterations divided |
| 401 | by the times that you are concerned with, not just the iterations. |
| 402 | .Sp |
| 403 | Returns: a Benchmark object. |
| 404 | .IP "disablecache ( )" 10 |
| 405 | .IX Item "disablecache ( )" |
| 406 | Disable caching of timings for the null loop. This will force Benchmark |
| 407 | to recalculate these timings for each new piece of code timed. |
| 408 | .IP "enablecache ( )" 10 |
| 409 | .IX Item "enablecache ( )" |
| 410 | Enable caching of timings for the null loop. The time taken for \s-1COUNT\s0 |
| 411 | rounds of the null loop will be calculated only once for each |
| 412 | different \s-1COUNT\s0 used. |
| 413 | .IP "timesum ( T1, T2 )" 10 |
| 414 | .IX Item "timesum ( T1, T2 )" |
| 415 | Returns the sum of two Benchmark times as a Benchmark object suitable |
| 416 | for passing to \fItimestr()\fR. |
| 417 | .Sh ":hireswallclock" |
| 418 | .IX Subsection ":hireswallclock" |
| 419 | If the Time::HiRes module has been installed, you can specify the |
| 420 | special tag \f(CW\*(C`:hireswallclock\*(C'\fR for Benchmark (if Time::HiRes is not |
| 421 | available, the tag will be silently ignored). This tag will cause the |
| 422 | wallclock time to be measured in microseconds, instead of integer |
| 423 | seconds. Note though that the speed computations are still conducted |
| 424 | in \s-1CPU\s0 time, not wallclock time. |
| 425 | .SH "NOTES" |
| 426 | .IX Header "NOTES" |
| 427 | The data is stored as a list of values from the time and times |
| 428 | functions: |
| 429 | .PP |
| 430 | .Vb 1 |
| 431 | \& ($real, $user, $system, $children_user, $children_system, $iters) |
| 432 | .Ve |
| 433 | .PP |
| 434 | in seconds for the whole loop (not divided by the number of rounds). |
| 435 | .PP |
| 436 | The timing is done using \fItime\fR\|(3) and \fItimes\fR\|(3). |
| 437 | .PP |
| 438 | Code is executed in the caller's package. |
| 439 | .PP |
| 440 | The time of the null loop (a loop with the same |
| 441 | number of rounds but empty loop body) is subtracted |
| 442 | from the time of the real loop. |
| 443 | .PP |
| 444 | The null loop times can be cached, the key being the |
| 445 | number of rounds. The caching can be controlled using |
| 446 | calls like these: |
| 447 | .PP |
| 448 | .Vb 2 |
| 449 | \& clearcache($key); |
| 450 | \& clearallcache(); |
| 451 | .Ve |
| 452 | .PP |
| 453 | .Vb 2 |
| 454 | \& disablecache(); |
| 455 | \& enablecache(); |
| 456 | .Ve |
| 457 | .PP |
| 458 | Caching is off by default, as it can (usually slightly) decrease |
| 459 | accuracy and does not usually noticably affect runtimes. |
| 460 | .SH "EXAMPLES" |
| 461 | .IX Header "EXAMPLES" |
| 462 | For example, |
| 463 | .PP |
| 464 | .Vb 6 |
| 465 | \& use Benchmark qw( cmpthese ) ; |
| 466 | \& $x = 3; |
| 467 | \& cmpthese( -5, { |
| 468 | \& a => sub{$x*$x}, |
| 469 | \& b => sub{$x**2}, |
| 470 | \& } ); |
| 471 | .Ve |
| 472 | .PP |
| 473 | outputs something like this: |
| 474 | .PP |
| 475 | .Vb 4 |
| 476 | \& Benchmark: running a, b, each for at least 5 CPU seconds... |
| 477 | \& Rate b a |
| 478 | \& b 1559428/s -- -62% |
| 479 | \& a 4152037/s 166% -- |
| 480 | .Ve |
| 481 | .PP |
| 482 | while |
| 483 | .PP |
| 484 | .Vb 7 |
| 485 | \& use Benchmark qw( timethese cmpthese ) ; |
| 486 | \& $x = 3; |
| 487 | \& $r = timethese( -5, { |
| 488 | \& a => sub{$x*$x}, |
| 489 | \& b => sub{$x**2}, |
| 490 | \& } ); |
| 491 | \& cmpthese $r; |
| 492 | .Ve |
| 493 | .PP |
| 494 | outputs something like this: |
| 495 | .PP |
| 496 | .Vb 6 |
| 497 | \& Benchmark: running a, b, each for at least 5 CPU seconds... |
| 498 | \& a: 10 wallclock secs ( 5.14 usr + 0.13 sys = 5.27 CPU) @ 3835055.60/s (n=20210743) |
| 499 | \& b: 5 wallclock secs ( 5.41 usr + 0.00 sys = 5.41 CPU) @ 1574944.92/s (n=8520452) |
| 500 | \& Rate b a |
| 501 | \& b 1574945/s -- -59% |
| 502 | \& a 3835056/s 144% -- |
| 503 | .Ve |
| 504 | .SH "INHERITANCE" |
| 505 | .IX Header "INHERITANCE" |
| 506 | Benchmark inherits from no other class, except of course |
| 507 | for Exporter. |
| 508 | .SH "CAVEATS" |
| 509 | .IX Header "CAVEATS" |
| 510 | Comparing eval'd strings with code references will give you |
| 511 | inaccurate results: a code reference will show a slightly slower |
| 512 | execution time than the equivalent eval'd string. |
| 513 | .PP |
| 514 | The real time timing is done using \fItime\fR\|(2) and |
| 515 | the granularity is therefore only one second. |
| 516 | .PP |
| 517 | Short tests may produce negative figures because perl |
| 518 | can appear to take longer to execute the empty loop |
| 519 | than a short test; try: |
| 520 | .PP |
| 521 | .Vb 1 |
| 522 | \& timethis(100,'1'); |
| 523 | .Ve |
| 524 | .PP |
| 525 | The system time of the null loop might be slightly |
| 526 | more than the system time of the loop with the actual |
| 527 | code and therefore the difference might end up being < 0. |
| 528 | .SH "SEE ALSO" |
| 529 | .IX Header "SEE ALSO" |
| 530 | Devel::DProf \- a Perl code profiler |
| 531 | .SH "AUTHORS" |
| 532 | .IX Header "AUTHORS" |
| 533 | Jarkko Hietaniemi <\fIjhi@iki.fi\fR>, Tim Bunce <\fITim.Bunce@ig.co.uk\fR> |
| 534 | .SH "MODIFICATION HISTORY" |
| 535 | .IX Header "MODIFICATION HISTORY" |
| 536 | September 8th, 1994; by Tim Bunce. |
| 537 | .PP |
| 538 | March 28th, 1997; by Hugo van der Sanden: added support for code |
| 539 | references and the already documented 'debug' method; revamped |
| 540 | documentation. |
| 541 | .PP |
| 542 | April 04\-07th, 1997: by Jarkko Hietaniemi, added the run-for-some-time |
| 543 | functionality. |
| 544 | .PP |
| 545 | September, 1999; by Barrie Slaymaker: math fixes and accuracy and |
| 546 | efficiency tweaks. Added \fIcmpthese()\fR. A result is now returned from |
| 547 | \&\fItimethese()\fR. Exposed \fIcountit()\fR (was \fIrunfor()\fR). |
| 548 | .PP |
| 549 | December, 2001; by Nicholas Clark: make \fItimestr()\fR recognise the style 'none' |
| 550 | and return an empty string. If cmpthese is calling timethese, make it pass the |
| 551 | style in. (so that 'none' will suppress output). Make sub new dump its |
| 552 | debugging output to \s-1STDERR\s0, to be consistent with everything else. |
| 553 | All bugs found while writing a regression test. |
| 554 | .PP |
| 555 | September, 2002; by Jarkko Hietaniemi: add ':hireswallclock' special tag. |
| 556 | .PP |
| 557 | February, 2004; by Chia-liang Kao: make cmpthese and timestr use time |
| 558 | statistics for children instead of parent when the style is 'nop'. |