Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Benchmark.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
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 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134Benchmark \- 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.SH "DESCRIPTION"
199.IX Header "DESCRIPTION"
200The Benchmark module encapsulates a number of routines to help you
201figure out how long it takes to execute some code.
202.PP
203timethis \- run a chunk of code several times
204.PP
205timethese \- run several chunks of code several times
206.PP
207cmpthese \- print results of timethese as a comparison chart
208.PP
209timeit \- run a chunk of code and see how long it goes
210.PP
211countit \- see how many times a chunk of code runs in a given time
212.Sh "Methods"
213.IX Subsection "Methods"
214.IP "new" 10
215.IX Item "new"
216Returns the current time. Example:
217.Sp
218.Vb 6
219\& use Benchmark;
220\& $t0 = new Benchmark;
221\& # ... your code here ...
222\& $t1 = new Benchmark;
223\& $td = timediff($t1, $t0);
224\& print "the code took:",timestr($td),"\en";
225.Ve
226.IP "debug" 10
227.IX Item "debug"
228Enables or disable debugging by setting the \f(CW$Benchmark::Debug\fR flag:
229.Sp
230.Vb 3
231\& debug Benchmark 1;
232\& $t = timeit(10, ' 5 ** $Global ');
233\& debug Benchmark 0;
234.Ve
235.IP "iters" 10
236.IX Item "iters"
237Returns the number of iterations.
238.Sh "Standard Exports"
239.IX Subsection "Standard Exports"
240The following routines will be exported into your namespace
241if you use the Benchmark module:
242.IP "timeit(\s-1COUNT\s0, \s-1CODE\s0)" 10
243.IX Item "timeit(COUNT, CODE)"
244Arguments: \s-1COUNT\s0 is the number of times to run the loop, and \s-1CODE\s0 is
245the code to run. \s-1CODE\s0 may be either a code reference or a string to
246be eval'd; either way it will be run in the caller's package.
247.Sp
248Returns: a Benchmark object.
249.IP "timethis ( \s-1COUNT\s0, \s-1CODE\s0, [ \s-1TITLE\s0, [ \s-1STYLE\s0 ]] )" 10
250.IX Item "timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] )"
251Time \s-1COUNT\s0 iterations of \s-1CODE\s0. \s-1CODE\s0 may be a string to eval or a
252code reference; either way the \s-1CODE\s0 will run in the caller's package.
253Results will be printed to \s-1STDOUT\s0 as \s-1TITLE\s0 followed by the times.
254\&\s-1TITLE\s0 defaults to \*(L"timethis \s-1COUNT\s0\*(R" if none is provided. \s-1STYLE\s0
255determines the format of the output, as described for \fItimestr()\fR below.
256.Sp
257The \s-1COUNT\s0 can be zero or negative: this means the \fIminimum number of
258\&\s-1CPU\s0 seconds\fR to run. A zero signifies the default of 3 seconds. For
259example to run at least for 10 seconds:
260.Sp
261.Vb 1
262\& timethis(-10, $code)
263.Ve
264.Sp
265or to run two pieces of code tests for at least 3 seconds:
266.Sp
267.Vb 1
268\& timethese(0, { test1 => '...', test2 => '...'})
269.Ve
270.Sp
271\&\s-1CPU\s0 seconds is, in \s-1UNIX\s0 terms, the user time plus the system time of
272the process itself, as opposed to the real (wallclock) time and the
273time spent by the child processes. Less than 0.1 seconds is not
274accepted (\-0.01 as the count, for example, will cause a fatal runtime
275exception).
276.Sp
277Note that the \s-1CPU\s0 seconds is the \fBminimum\fR time: \s-1CPU\s0 scheduling and
278other operating system factors may complicate the attempt so that a
279little bit more time is spent. The benchmark output will, however,
280also tell the number of \f(CW$code\fR runs/second, which should be a more
281interesting number than the actually spent seconds.
282.Sp
283Returns a Benchmark object.
284.IP "timethese ( \s-1COUNT\s0, \s-1CODEHASHREF\s0, [ \s-1STYLE\s0 ] )" 10
285.IX Item "timethese ( COUNT, CODEHASHREF, [ STYLE ] )"
286The \s-1CODEHASHREF\s0 is a reference to a hash containing names as keys
287and either a string to eval or a code reference for each value.
288For each (\s-1KEY\s0, \s-1VALUE\s0) pair in the \s-1CODEHASHREF\s0, this routine will
289call
290.Sp
291.Vb 1
292\& timethis(COUNT, VALUE, KEY, STYLE)
293.Ve
294.Sp
295The routines are called in string comparison order of \s-1KEY\s0.
296.Sp
297The \s-1COUNT\s0 can be zero or negative, see \fItimethis()\fR.
298.Sp
299Returns a hash of Benchmark objects, keyed by name.
300.IP "timediff ( T1, T2 )" 10
301.IX Item "timediff ( T1, T2 )"
302Returns the difference between two Benchmark times as a Benchmark
303object suitable for passing to \fItimestr()\fR.
304.IP "timestr ( \s-1TIMEDIFF\s0, [ \s-1STYLE\s0, [ \s-1FORMAT\s0 ] ] )" 10
305.IX Item "timestr ( TIMEDIFF, [ STYLE, [ FORMAT ] ] )"
306Returns a string that formats the times in the \s-1TIMEDIFF\s0 object in
307the requested \s-1STYLE\s0. \s-1TIMEDIFF\s0 is expected to be a Benchmark object
308similar to that returned by \fItimediff()\fR.
309.Sp
310\&\s-1STYLE\s0 can be any of 'all', 'none', 'noc', 'nop' or 'auto'. 'all' shows
311each of the 5 times available ('wallclock' time, user time, system time,
312user time of children, and system time of children). 'noc' shows all
313except the two children times. 'nop' shows only wallclock and the
314two children times. 'auto' (the default) will act as 'all' unless
315the children times are both zero, in which case it acts as 'noc'.
316\&'none' prevents output.
317.Sp
318\&\s-1FORMAT\s0 is the \fIprintf\fR\|(3)\-style format specifier (without the
319leading '%') to use to print the times. It defaults to '5.2f'.
320.Sh "Optional Exports"
321.IX Subsection "Optional Exports"
322The following routines will be exported into your namespace
323if you specifically ask that they be imported:
324.IP "clearcache ( \s-1COUNT\s0 )" 10
325.IX Item "clearcache ( COUNT )"
326Clear the cached time for \s-1COUNT\s0 rounds of the null loop.
327.IP "clearallcache ( )" 10
328.IX Item "clearallcache ( )"
329Clear all cached times.
330.IP "cmpthese ( \s-1COUT\s0, \s-1CODEHASHREF\s0, [ \s-1STYLE\s0 ] )" 10
331.IX Item "cmpthese ( COUT, CODEHASHREF, [ STYLE ] )"
332.PD 0
333.IP "cmpthese ( \s-1RESULTSHASHREF\s0, [ \s-1STYLE\s0 ] )" 10
334.IX Item "cmpthese ( RESULTSHASHREF, [ STYLE ] )"
335.PD
336Optionally calls \fItimethese()\fR, then outputs comparison chart. This:
337.Sp
338.Vb 1
339\& cmpthese( -1, { a => "++\e$i", b => "\e$i *= 2" } ) ;
340.Ve
341.Sp
342outputs a chart like:
343.Sp
344.Vb 3
345\& Rate b a
346\& b 2831802/s -- -61%
347\& a 7208959/s 155% --
348.Ve
349.Sp
350This chart is sorted from slowest to fastest, and shows the percent speed
351difference between each pair of tests.
352.Sp
353c<cmpthese> can also be passed the data structure that \fItimethese()\fR returns:
354.Sp
355.Vb 2
356\& $results = timethese( -1, { a => "++\e$i", b => "\e$i *= 2" } ) ;
357\& cmpthese( $results );
358.Ve
359.Sp
360in case you want to see both sets of results.
361.Sp
362Returns a reference to an \s-1ARRAY\s0 of rows, each row is an \s-1ARRAY\s0 of cells from the
363above chart, including labels. This:
364.Sp
365.Vb 1
366\& my $rows = cmpthese( -1, { a => '++$i', b => '$i *= 2' }, "none" );
367.Ve
368.Sp
369returns a data structure like:
370.Sp
371.Vb 5
372\& [
373\& [ '', 'Rate', 'b', 'a' ],
374\& [ 'b', '2885232/s', '--', '-59%' ],
375\& [ 'a', '7099126/s', '146%', '--' ],
376\& ]
377.Ve
378.Sp
379\&\fB\s-1NOTE\s0\fR: This result value differs from previous versions, which returned
380the \f(CW\*(C`timethese()\*(C'\fR result structure. If you want that, just use the two
381statement \f(CW\*(C`timethese\*(C'\fR...\f(CW\*(C`cmpthese\*(C'\fR idiom shown above.
382.Sp
383Incidently, note the variance in the result values between the two examples;
384this is typical of benchmarking. If this were a real benchmark, you would
385probably want to run a lot more iterations.
386.IP "countit(\s-1TIME\s0, \s-1CODE\s0)" 10
387.IX Item "countit(TIME, CODE)"
388Arguments: \s-1TIME\s0 is the minimum length of time to run \s-1CODE\s0 for, and \s-1CODE\s0 is
389the code to run. \s-1CODE\s0 may be either a code reference or a string to
390be eval'd; either way it will be run in the caller's package.
391.Sp
392\&\s-1TIME\s0 is \fInot\fR negative. \fIcountit()\fR will run the loop many times to
393calculate the speed of \s-1CODE\s0 before running it for \s-1TIME\s0. The actual
394time run for will usually be greater than \s-1TIME\s0 due to system clock
395resolution, so it's best to look at the number of iterations divided
396by the times that you are concerned with, not just the iterations.
397.Sp
398Returns: a Benchmark object.
399.IP "disablecache ( )" 10
400.IX Item "disablecache ( )"
401Disable caching of timings for the null loop. This will force Benchmark
402to recalculate these timings for each new piece of code timed.
403.IP "enablecache ( )" 10
404.IX Item "enablecache ( )"
405Enable caching of timings for the null loop. The time taken for \s-1COUNT\s0
406rounds of the null loop will be calculated only once for each
407different \s-1COUNT\s0 used.
408.IP "timesum ( T1, T2 )" 10
409.IX Item "timesum ( T1, T2 )"
410Returns the sum of two Benchmark times as a Benchmark object suitable
411for passing to \fItimestr()\fR.
412.SH "NOTES"
413.IX Header "NOTES"
414The data is stored as a list of values from the time and times
415functions:
416.PP
417.Vb 1
418\& ($real, $user, $system, $children_user, $children_system, $iters)
419.Ve
420.PP
421in seconds for the whole loop (not divided by the number of rounds).
422.PP
423The timing is done using \fItime\fR\|(3) and \fItimes\fR\|(3).
424.PP
425Code is executed in the caller's package.
426.PP
427The time of the null loop (a loop with the same
428number of rounds but empty loop body) is subtracted
429from the time of the real loop.
430.PP
431The null loop times can be cached, the key being the
432number of rounds. The caching can be controlled using
433calls like these:
434.PP
435.Vb 2
436\& clearcache($key);
437\& clearallcache();
438.Ve
439.PP
440.Vb 2
441\& disablecache();
442\& enablecache();
443.Ve
444.PP
445Caching is off by default, as it can (usually slightly) decrease
446accuracy and does not usually noticably affect runtimes.
447.SH "EXAMPLES"
448.IX Header "EXAMPLES"
449For example,
450.PP
451.Vb 6
452\& use Benchmark qw( cmpthese ) ;
453\& $x = 3;
454\& cmpthese( -5, {
455\& a => sub{$x*$x},
456\& b => sub{$x**2},
457\& } );
458.Ve
459.PP
460outputs something like this:
461.PP
462.Vb 4
463\& Benchmark: running a, b, each for at least 5 CPU seconds...
464\& Rate b a
465\& b 1559428/s -- -62%
466\& a 4152037/s 166% --
467.Ve
468.PP
469while
470.PP
471.Vb 7
472\& use Benchmark qw( timethese cmpthese ) ;
473\& $x = 3;
474\& $r = timethese( -5, {
475\& a => sub{$x*$x},
476\& b => sub{$x**2},
477\& } );
478\& cmpthese $r;
479.Ve
480.PP
481outputs something like this:
482.PP
483.Vb 6
484\& Benchmark: running a, b, each for at least 5 CPU seconds...
485\& a: 10 wallclock secs ( 5.14 usr + 0.13 sys = 5.27 CPU) @ 3835055.60/s (n=20210743)
486\& b: 5 wallclock secs ( 5.41 usr + 0.00 sys = 5.41 CPU) @ 1574944.92/s (n=8520452)
487\& Rate b a
488\& b 1574945/s -- -59%
489\& a 3835056/s 144% --
490.Ve
491.SH "INHERITANCE"
492.IX Header "INHERITANCE"
493Benchmark inherits from no other class, except of course
494for Exporter.
495.SH "CAVEATS"
496.IX Header "CAVEATS"
497Comparing eval'd strings with code references will give you
498inaccurate results: a code reference will show a slightly slower
499execution time than the equivalent eval'd string.
500.PP
501The real time timing is done using \fItime\fR\|(2) and
502the granularity is therefore only one second.
503.PP
504Short tests may produce negative figures because perl
505can appear to take longer to execute the empty loop
506than a short test; try:
507.PP
508.Vb 1
509\& timethis(100,'1');
510.Ve
511.PP
512The system time of the null loop might be slightly
513more than the system time of the loop with the actual
514code and therefore the difference might end up being < 0.
515.SH "SEE ALSO"
516.IX Header "SEE ALSO"
517Devel::DProf \- a Perl code profiler
518.SH "AUTHORS"
519.IX Header "AUTHORS"
520Jarkko Hietaniemi <\fIjhi@iki.fi\fR>, Tim Bunce <\fITim.Bunce@ig.co.uk\fR>
521.SH "MODIFICATION HISTORY"
522.IX Header "MODIFICATION HISTORY"
523September 8th, 1994; by Tim Bunce.
524.PP
525March 28th, 1997; by Hugo van der Sanden: added support for code
526references and the already documented 'debug' method; revamped
527documentation.
528.PP
529April 04\-07th, 1997: by Jarkko Hietaniemi, added the run-for-some-time
530functionality.
531.PP
532September, 1999; by Barrie Slaymaker: math fixes and accuracy and
533efficiency tweaks. Added \fIcmpthese()\fR. A result is now returned from
534\&\fItimethese()\fR. Exposed \fIcountit()\fR (was \fIrunfor()\fR).
535.PP
536December, 2001; by Nicholas Clark: make \fItimestr()\fR recognise the style 'none'
537and return an empty string. If cmpthese is calling timethese, make it pass the
538style in. (so that 'none' will suppress output). Make sub new dump its
539debugging output to \s-1STDERR\s0, to be consistent with everything else.
540All bugs found while writing a regression test.