← Index
NYTProf Performance Profile   « line view »
For examples/Atom-timer.pl
  Run on Mon Aug 12 14:45:28 2013
Reported on Mon Aug 12 14:46:14 2013

Filename/Users/dde/perl5/perlbrew/perls/5.18.0t/lib/5.18.0/Benchmark.pm
StatementsExecuted 112104 statements in 194ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1000001140.3ms40.3msBenchmark::::__ANON__[:687]Benchmark::__ANON__[:687]
29303110.7ms12.3msBenchmark::::newBenchmark::new
4216.01ms6.68sBenchmark::::runloopBenchmark::runloop
2930111.64ms1.64msBenchmark::::mytimeBenchmark::mytime
111108µs6.68sBenchmark::::cmptheseBenchmark::cmpthese
21152µs6.68sBenchmark::::timeitBenchmark::timeit
62145µs45µsBenchmark::::timediffBenchmark::timediff
21140µs6.68sBenchmark::::timethisBenchmark::timethis
11126µs6.68sBenchmark::::timetheseBenchmark::timethese
11121µs79µsBenchmark::::BEGIN@453Benchmark::BEGIN@453
31117µs17µsBenchmark::::CORE:prtfBenchmark::CORE:prtf (opcode)
104112µs12µsBenchmark::::timedebugBenchmark::timedebug
11110µs20µsBenchmark::::BEGIN@3Benchmark::BEGIN@3
11110µs12µsBenchmark::::initBenchmark::init
1119µs91µsBenchmark::::importBenchmark::import
3317µs7µsBenchmark::::CORE:sortBenchmark::CORE:sort (opcode)
1117µs38µsBenchmark::::BEGIN@432Benchmark::BEGIN@432
1116µs19µsBenchmark::::BEGIN@433Benchmark::BEGIN@433
2116µs6µsBenchmark::::cpu_aBenchmark::cpu_a
1116µs16µsBenchmark::::BEGIN@426Benchmark::BEGIN@426
2114µs4µsBenchmark::::realBenchmark::real
1111µs1µsBenchmark::::clearallcacheBenchmark::clearallcache
111800ns800nsBenchmark::::disablecacheBenchmark::disablecache
0000s0sBenchmark::::_doevalBenchmark::_doeval
0000s0sBenchmark::::clearcacheBenchmark::clearcache
0000s0sBenchmark::::countitBenchmark::countit
0000s0sBenchmark::::cpu_cBenchmark::cpu_c
0000s0sBenchmark::::cpu_pBenchmark::cpu_p
0000s0sBenchmark::::debugBenchmark::debug
0000s0sBenchmark::::enablecacheBenchmark::enablecache
0000s0sBenchmark::::itersBenchmark::iters
0000s0sBenchmark::::n_to_forBenchmark::n_to_for
0000s0sBenchmark::::timestrBenchmark::timestr
0000s0sBenchmark::::timesumBenchmark::timesum
0000s0sBenchmark::::usageBenchmark::usage
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Benchmark;
2
32202µs231µs
# spent 20µs (10+11) within Benchmark::BEGIN@3 which was called: # once (10µs+11µs) by main::BEGIN@4 at line 3
use strict;
# spent 20µs making 1 call to Benchmark::BEGIN@3 # spent 11µs making 1 call to strict::import
4
5
6=head1 NAME
7
8Benchmark - benchmark running times of Perl code
9
10=head1 SYNOPSIS
11
12 use Benchmark qw(:all) ;
13
14 timethis ($count, "code");
15
16 # Use Perl code in strings...
17 timethese($count, {
18 'Name1' => '...code1...',
19 'Name2' => '...code2...',
20 });
21
22 # ... or use subroutine references.
23 timethese($count, {
24 'Name1' => sub { ...code1... },
25 'Name2' => sub { ...code2... },
26 });
27
28 # cmpthese can be used both ways as well
29 cmpthese($count, {
30 'Name1' => '...code1...',
31 'Name2' => '...code2...',
32 });
33
34 cmpthese($count, {
35 'Name1' => sub { ...code1... },
36 'Name2' => sub { ...code2... },
37 });
38
39 # ...or in two stages
40 $results = timethese($count,
41 {
42 'Name1' => sub { ...code1... },
43 'Name2' => sub { ...code2... },
44 },
45 'none'
46 );
47 cmpthese( $results ) ;
48
49 $t = timeit($count, '...other code...')
50 print "$count loops of other code took:",timestr($t),"\n";
51
52 $t = countit($time, '...other code...')
53 $count = $t->iters ;
54 print "$count loops of other code took:",timestr($t),"\n";
55
56 # enable hires wallclock timing if possible
57 use Benchmark ':hireswallclock';
58
59=head1 DESCRIPTION
60
61The Benchmark module encapsulates a number of routines to help you
62figure out how long it takes to execute some code.
63
64timethis - run a chunk of code several times
65
66timethese - run several chunks of code several times
67
68cmpthese - print results of timethese as a comparison chart
69
70timeit - run a chunk of code and see how long it goes
71
72countit - see how many times a chunk of code runs in a given time
73
74
75=head2 Methods
76
77=over 10
78
79=item new
80
81Returns the current time. Example:
82
83 use Benchmark;
84 $t0 = Benchmark->new;
85 # ... your code here ...
86 $t1 = Benchmark->new;
87 $td = timediff($t1, $t0);
88 print "the code took:",timestr($td),"\n";
89
90=item debug
91
92Enables or disable debugging by setting the C<$Benchmark::Debug> flag:
93
94 Benchmark->debug(1);
95 $t = timeit(10, ' 5 ** $Global ');
96 Benchmark->debug(0);
97
98=item iters
99
100Returns the number of iterations.
101
102=back
103
104=head2 Standard Exports
105
106The following routines will be exported into your namespace
107if you use the Benchmark module:
108
109=over 10
110
111=item timeit(COUNT, CODE)
112
113Arguments: COUNT is the number of times to run the loop, and CODE is
114the code to run. CODE may be either a code reference or a string to
115be eval'd; either way it will be run in the caller's package.
116
117Returns: a Benchmark object.
118
119=item timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] )
120
121Time COUNT iterations of CODE. CODE may be a string to eval or a
122code reference; either way the CODE will run in the caller's package.
123Results will be printed to STDOUT as TITLE followed by the times.
124TITLE defaults to "timethis COUNT" if none is provided. STYLE
125determines the format of the output, as described for timestr() below.
126
127The COUNT can be zero or negative: this means the I<minimum number of
128CPU seconds> to run. A zero signifies the default of 3 seconds. For
129example to run at least for 10 seconds:
130
131 timethis(-10, $code)
132
133or to run two pieces of code tests for at least 3 seconds:
134
135 timethese(0, { test1 => '...', test2 => '...'})
136
137CPU seconds is, in UNIX terms, the user time plus the system time of
138the process itself, as opposed to the real (wallclock) time and the
139time spent by the child processes. Less than 0.1 seconds is not
140accepted (-0.01 as the count, for example, will cause a fatal runtime
141exception).
142
143Note that the CPU seconds is the B<minimum> time: CPU scheduling and
144other operating system factors may complicate the attempt so that a
145little bit more time is spent. The benchmark output will, however,
146also tell the number of C<$code> runs/second, which should be a more
147interesting number than the actually spent seconds.
148
149Returns a Benchmark object.
150
151=item timethese ( COUNT, CODEHASHREF, [ STYLE ] )
152
153The CODEHASHREF is a reference to a hash containing names as keys
154and either a string to eval or a code reference for each value.
155For each (KEY, VALUE) pair in the CODEHASHREF, this routine will
156call
157
158 timethis(COUNT, VALUE, KEY, STYLE)
159
160The routines are called in string comparison order of KEY.
161
162The COUNT can be zero or negative, see timethis().
163
164Returns a hash reference of Benchmark objects, keyed by name.
165
166=item timediff ( T1, T2 )
167
168Returns the difference between two Benchmark times as a Benchmark
169object suitable for passing to timestr().
170
171=item timestr ( TIMEDIFF, [ STYLE, [ FORMAT ] ] )
172
173Returns a string that formats the times in the TIMEDIFF object in
174the requested STYLE. TIMEDIFF is expected to be a Benchmark object
175similar to that returned by timediff().
176
177STYLE can be any of 'all', 'none', 'noc', 'nop' or 'auto'. 'all' shows
178each of the 5 times available ('wallclock' time, user time, system time,
179user time of children, and system time of children). 'noc' shows all
180except the two children times. 'nop' shows only wallclock and the
181two children times. 'auto' (the default) will act as 'all' unless
182the children times are both zero, in which case it acts as 'noc'.
183'none' prevents output.
184
185FORMAT is the L<printf(3)>-style format specifier (without the
186leading '%') to use to print the times. It defaults to '5.2f'.
187
188=back
189
190=head2 Optional Exports
191
192The following routines will be exported into your namespace
193if you specifically ask that they be imported:
194
195=over 10
196
197=item clearcache ( COUNT )
198
199Clear the cached time for COUNT rounds of the null loop.
200
201=item clearallcache ( )
202
203Clear all cached times.
204
205=item cmpthese ( COUNT, CODEHASHREF, [ STYLE ] )
206
207=item cmpthese ( RESULTSHASHREF, [ STYLE ] )
208
209Optionally calls timethese(), then outputs comparison chart. This:
210
211 cmpthese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
212
213outputs a chart like:
214
215 Rate b a
216 b 2831802/s -- -61%
217 a 7208959/s 155% --
218
219This chart is sorted from slowest to fastest, and shows the percent speed
220difference between each pair of tests.
221
222C<cmpthese> can also be passed the data structure that timethese() returns:
223
224 $results = timethese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
225 cmpthese( $results );
226
227in case you want to see both sets of results.
228If the first argument is an unblessed hash reference,
229that is RESULTSHASHREF; otherwise that is COUNT.
230
231Returns a reference to an ARRAY of rows, each row is an ARRAY of cells from the
232above chart, including labels. This:
233
234 my $rows = cmpthese( -1, { a => '++$i', b => '$i *= 2' }, "none" );
235
236returns a data structure like:
237
238 [
239 [ '', 'Rate', 'b', 'a' ],
240 [ 'b', '2885232/s', '--', '-59%' ],
241 [ 'a', '7099126/s', '146%', '--' ],
242 ]
243
244B<NOTE>: This result value differs from previous versions, which returned
245the C<timethese()> result structure. If you want that, just use the two
246statement C<timethese>...C<cmpthese> idiom shown above.
247
248Incidentally, note the variance in the result values between the two examples;
249this is typical of benchmarking. If this were a real benchmark, you would
250probably want to run a lot more iterations.
251
252=item countit(TIME, CODE)
253
254Arguments: TIME is the minimum length of time to run CODE for, and CODE is
255the code to run. CODE may be either a code reference or a string to
256be eval'd; either way it will be run in the caller's package.
257
258TIME is I<not> negative. countit() will run the loop many times to
259calculate the speed of CODE before running it for TIME. The actual
260time run for will usually be greater than TIME due to system clock
261resolution, so it's best to look at the number of iterations divided
262by the times that you are concerned with, not just the iterations.
263
264Returns: a Benchmark object.
265
266=item disablecache ( )
267
268Disable caching of timings for the null loop. This will force Benchmark
269to recalculate these timings for each new piece of code timed.
270
271=item enablecache ( )
272
273Enable caching of timings for the null loop. The time taken for COUNT
274rounds of the null loop will be calculated only once for each
275different COUNT used.
276
277=item timesum ( T1, T2 )
278
279Returns the sum of two Benchmark times as a Benchmark object suitable
280for passing to timestr().
281
282=back
283
284=head2 :hireswallclock
285
286If the Time::HiRes module has been installed, you can specify the
287special tag C<:hireswallclock> for Benchmark (if Time::HiRes is not
288available, the tag will be silently ignored). This tag will cause the
289wallclock time to be measured in microseconds, instead of integer
290seconds. Note though that the speed computations are still conducted
291in CPU time, not wallclock time.
292
293=head1 NOTES
294
295The data is stored as a list of values from the time and times
296functions:
297
298 ($real, $user, $system, $children_user, $children_system, $iters)
299
300in seconds for the whole loop (not divided by the number of rounds).
301
302The timing is done using time(3) and times(3).
303
304Code is executed in the caller's package.
305
306The time of the null loop (a loop with the same
307number of rounds but empty loop body) is subtracted
308from the time of the real loop.
309
310The null loop times can be cached, the key being the
311number of rounds. The caching can be controlled using
312calls like these:
313
314 clearcache($key);
315 clearallcache();
316
317 disablecache();
318 enablecache();
319
320Caching is off by default, as it can (usually slightly) decrease
321accuracy and does not usually noticeably affect runtimes.
322
323=head1 EXAMPLES
324
325For example,
326
327 use Benchmark qw( cmpthese ) ;
328 $x = 3;
329 cmpthese( -5, {
330 a => sub{$x*$x},
331 b => sub{$x**2},
332 } );
333
334outputs something like this:
335
336 Benchmark: running a, b, each for at least 5 CPU seconds...
337 Rate b a
338 b 1559428/s -- -62%
339 a 4152037/s 166% --
340
341
342while
343
344 use Benchmark qw( timethese cmpthese ) ;
345 $x = 3;
346 $r = timethese( -5, {
347 a => sub{$x*$x},
348 b => sub{$x**2},
349 } );
350 cmpthese $r;
351
352outputs something like this:
353
354 Benchmark: running a, b, each for at least 5 CPU seconds...
355 a: 10 wallclock secs ( 5.14 usr + 0.13 sys = 5.27 CPU) @ 3835055.60/s (n=20210743)
356 b: 5 wallclock secs ( 5.41 usr + 0.00 sys = 5.41 CPU) @ 1574944.92/s (n=8520452)
357 Rate b a
358 b 1574945/s -- -59%
359 a 3835056/s 144% --
360
361
362=head1 INHERITANCE
363
364Benchmark inherits from no other class, except of course
365for Exporter.
366
367=head1 CAVEATS
368
369Comparing eval'd strings with code references will give you
370inaccurate results: a code reference will show a slightly slower
371execution time than the equivalent eval'd string.
372
373The real time timing is done using time(2) and
374the granularity is therefore only one second.
375
376Short tests may produce negative figures because perl
377can appear to take longer to execute the empty loop
378than a short test; try:
379
380 timethis(100,'1');
381
382The system time of the null loop might be slightly
383more than the system time of the loop with the actual
384code and therefore the difference might end up being E<lt> 0.
385
386=head1 SEE ALSO
387
388L<Devel::NYTProf> - a Perl code profiler
389
390=head1 AUTHORS
391
392Jarkko Hietaniemi <F<jhi@iki.fi>>, Tim Bunce <F<Tim.Bunce@ig.co.uk>>
393
394=head1 MODIFICATION HISTORY
395
396September 8th, 1994; by Tim Bunce.
397
398March 28th, 1997; by Hugo van der Sanden: added support for code
399references and the already documented 'debug' method; revamped
400documentation.
401
402April 04-07th, 1997: by Jarkko Hietaniemi, added the run-for-some-time
403functionality.
404
405September, 1999; by Barrie Slaymaker: math fixes and accuracy and
406efficiency tweaks. Added cmpthese(). A result is now returned from
407timethese(). Exposed countit() (was runfor()).
408
409December, 2001; by Nicholas Clark: make timestr() recognise the style 'none'
410and return an empty string. If cmpthese is calling timethese, make it pass the
411style in. (so that 'none' will suppress output). Make sub new dump its
412debugging output to STDERR, to be consistent with everything else.
413All bugs found while writing a regression test.
414
415September, 2002; by Jarkko Hietaniemi: add ':hireswallclock' special tag.
416
417February, 2004; by Chia-liang Kao: make cmpthese and timestr use time
418statistics for children instead of parent when the style is 'nop'.
419
420November, 2007; by Christophe Grosjean: make cmpthese and timestr compute
421time consistently with style argument, default is 'all' not 'noc' any more.
422
423=cut
424
425# evaluate something in a clean lexical environment
426228µs225µs
# spent 16µs (6+10) within Benchmark::BEGIN@426 which was called: # once (6µs+10µs) by main::BEGIN@4 at line 426
sub _doeval { no strict; eval shift }
# spent 16µs making 1 call to Benchmark::BEGIN@426 # spent 10µs making 1 call to strict::unimport
427
428#
429# put any lexicals at file scope AFTER here
430#
431
432220µs270µs
# spent 38µs (7+32) within Benchmark::BEGIN@432 which was called: # once (7µs+32µs) by main::BEGIN@4 at line 432
use Carp;
# spent 38µs making 1 call to Benchmark::BEGIN@432 # spent 32µs making 1 call to Exporter::import
4332114µs231µs
# spent 19µs (6+12) within Benchmark::BEGIN@433 which was called: # once (6µs+12µs) by main::BEGIN@4 at line 433
use Exporter;
# spent 19µs making 1 call to Benchmark::BEGIN@433 # spent 12µs making 1 call to Exporter::import
434
4351400nsour(@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
436
43715µs@ISA=qw(Exporter);
43811µs@EXPORT=qw(timeit timethis timethese timediff timestr);
43911µs@EXPORT_OK=qw(timesum cmpthese countit
440 clearcache clearallcache disablecache enablecache);
44113µs%EXPORT_TAGS=( all => [ @EXPORT, @EXPORT_OK ] ) ;
442
4431200ns$VERSION = 1.15;
444
445# --- ':hireswallclock' special handling
446
44710smy $hirestime;
448
44929303.75ms
# spent 1.64ms within Benchmark::mytime which was called 2930 times, avg 558ns/call: # 2930 times (1.64ms+0s) by Benchmark::new at line 536, avg 558ns/call
sub mytime () { time }
450
45111µs112µsinit();
# spent 12µs making 1 call to Benchmark::init
452
453
# spent 79µs (21+58) within Benchmark::BEGIN@453 which was called: # once (21µs+58µs) by main::BEGIN@4 at line 458
sub BEGIN {
454121µs if (eval 'require Time::HiRes') {
# spent 2µs executing statements in string eval
45511µs158µs import Time::HiRes qw(time);
# spent 58µs making 1 call to Time::HiRes::import
4561400ns $hirestime = \&Time::HiRes::time;
457 }
45812.48ms179µs}
# spent 79µs making 1 call to Benchmark::BEGIN@453
459
460
# spent 91µs (9+81) within Benchmark::import which was called: # once (9µs+81µs) by main::BEGIN@4 at line 4 of examples/Atom-timer.pl
sub import {
4611300ns my $class = shift;
4621900ns if (grep { $_ eq ":hireswallclock" } @_) {
463 @_ = grep { $_ ne ":hireswallclock" } @_;
464 local $^W=0;
465 *mytime = $hirestime if defined $hirestime;
466 }
46715µs111µs Benchmark->export_to_level(1, $class, @_);
# spent 11µs making 1 call to Exporter::export_to_level
468}
469
4701200nsour($Debug, $Min_Count, $Min_CPU, $Default_Format, $Default_Style,
471 %_Usage, %Cache, $Do_Cache);
472
473
# spent 12µs (10+2) within Benchmark::init which was called: # once (10µs+2µs) by main::BEGIN@4 at line 451
sub init {
4741100ns $Debug = 0;
4751100ns $Min_Count = 4;
4761100ns $Min_CPU = 0.4;
4771200ns $Default_Format = '5.2f';
4781100ns $Default_Style = 'auto';
479 # The cache can cause a slight loss of sys time accuracy. If a
480 # user does many tests (>10) with *very* large counts (>10000)
481 # or works on a very slow machine the cache may be useful.
4821500ns1800ns disablecache();
# spent 800ns making 1 call to Benchmark::disablecache
48313µs11µs clearallcache();
# spent 1µs making 1 call to Benchmark::clearallcache
484}
485
486sub debug { $Debug = ($_[1] != 0); }
487
488sub usage {
489 my $calling_sub = (caller(1))[3];
490 $calling_sub =~ s/^Benchmark:://;
491 return $_Usage{$calling_sub} || '';
492}
493
494# The cache needs two branches: 's' for strings and 'c' for code. The
495# empty loop is different in these two cases.
496
4971600ns$_Usage{clearcache} = <<'USAGE';
498usage: clearcache($count);
499USAGE
500
501sub clearcache {
502 die usage unless @_ == 1;
503 delete $Cache{"$_[0]c"}; delete $Cache{"$_[0]s"};
504}
505
5061200ns$_Usage{clearallcache} = <<'USAGE';
507usage: clearallcache();
508USAGE
509
510
# spent 1µs within Benchmark::clearallcache which was called: # once (1µs+0s) by Benchmark::init at line 483
sub clearallcache {
5111100ns die usage if @_;
51213µs %Cache = ();
513}
514
5151200ns$_Usage{enablecache} = <<'USAGE';
516usage: enablecache();
517USAGE
518
519sub enablecache {
520 die usage if @_;
521 $Do_Cache = 1;
522}
523
5241200ns$_Usage{disablecache} = <<'USAGE';
525usage: disablecache();
526USAGE
527
528
# spent 800ns within Benchmark::disablecache which was called: # once (800ns+0s) by Benchmark::init at line 482
sub disablecache {
5291200ns die usage if @_;
53014µs $Do_Cache = 0;
531}
532
533
534# --- Functions to process the 'time' data type
535
53629304.92ms29301.64ms
# spent 12.3ms (10.7+1.64) within Benchmark::new which was called 2930 times, avg 4µs/call: # 2922 times (10.6ms+1.62ms) by Benchmark::runloop at line 662, avg 4µs/call # 4 times (33µs+9µs) by Benchmark::runloop at line 661, avg 10µs/call # 4 times (34µs+7µs) by Benchmark::runloop at line 664, avg 10µs/call
sub new { my @t = (mytime, times, @_ == 2 ? $_[1] : 0);
# spent 1.64ms making 2930 calls to Benchmark::mytime, avg 558ns/call
53729301µs print STDERR "new=@t\n" if $Debug;
53829303.29ms bless \@t; }
539
540sub cpu_p { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps ; }
541sub cpu_c { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $cu+$cs ; }
54247µs
# spent 6µs within Benchmark::cpu_a which was called 2 times, avg 3µs/call: # 2 times (6µs+0s) by Benchmark::timethis at line 839, avg 3µs/call
sub cpu_a { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps+$cu+$cs ; }
54346µs
# spent 4µs within Benchmark::real which was called 2 times, avg 2µs/call: # 2 times (4µs+0s) by Benchmark::timethis at line 839, avg 2µs/call
sub real { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $r ; }
544sub iters { $_[0]->[5] ; }
545
546
5471200ns$_Usage{timediff} = <<'USAGE';
548usage: $result_diff = timediff($result1, $result2);
549USAGE
550
551
# spent 45µs within Benchmark::timediff which was called 6 times, avg 8µs/call: # 4 times (35µs+0s) by Benchmark::runloop at line 665, avg 9µs/call # 2 times (10µs+0s) by Benchmark::timeit at line 696, avg 5µs/call
sub timediff {
55262µs my($a, $b) = @_;
553
55463µs die usage unless ref $a and ref $b;
555
5566800ns my @r;
557623µs for (my $i=0; $i < @$a; ++$i) {
558 push(@r, $a->[$i] - $b->[$i]);
559 }
560 #die "Bad timediff(): ($r[1] + $r[2]) <= 0 (@$a[1,2]|@$b[1,2])\n"
561 # if ($r[1] + $r[2]) < 0;
562613µs bless \@r;
563}
564
5651200ns$_Usage{timesum} = <<'USAGE';
566usage: $sum = timesum($result1, $result2);
567USAGE
568
569sub timesum {
570 my($a, $b) = @_;
571
572 die usage unless ref $a and ref $b;
573
574 my @r;
575 for (my $i=0; $i < @$a; ++$i) {
576 push(@r, $a->[$i] + $b->[$i]);
577 }
578 bless \@r;
579}
580
581
5821200ns$_Usage{timestr} = <<'USAGE';
583usage: $formatted_result = timestr($result1);
584USAGE
585
586sub timestr {
587 my($tr, $style, $f) = @_;
588
589 die usage unless ref $tr;
590
591 my @t = @$tr;
592 warn "bad time value (@t)" unless @t==6;
593 my($r, $pu, $ps, $cu, $cs, $n) = @t;
594 my($pt, $ct, $tt) = ($tr->cpu_p, $tr->cpu_c, $tr->cpu_a);
595 $f = $Default_Format unless defined $f;
596 # format a time in the required style, other formats may be added here
597 $style ||= $Default_Style;
598 return '' if $style eq 'none';
599 $style = ($ct>0) ? 'all' : 'noc' if $style eq 'auto';
600 my $s = "@t $style"; # default for unknown style
601 my $w = $hirestime ? "%2g" : "%2d";
602 $s = sprintf("$w wallclock secs (%$f usr %$f sys + %$f cusr %$f csys = %$f CPU)",
603 $r,$pu,$ps,$cu,$cs,$tt) if $style eq 'all';
604 $s = sprintf("$w wallclock secs (%$f usr + %$f sys = %$f CPU)",
605 $r,$pu,$ps,$pt) if $style eq 'noc';
606 $s = sprintf("$w wallclock secs (%$f cusr + %$f csys = %$f CPU)",
607 $r,$cu,$cs,$ct) if $style eq 'nop';
608 my $elapsed = do {
609 if ($style eq 'nop') {$cu+$cs}
610 elsif ($style eq 'noc') {$pu+$ps}
611 else {$cu+$cs+$pu+$ps}
612 };
613 $s .= sprintf(" @ %$f/s (n=$n)",$n/($elapsed)) if $n && $elapsed;
614 $s;
615}
616
617
# spent 12µs within Benchmark::timedebug which was called 10 times, avg 1µs/call: # 4 times (7µs+0s) by Benchmark::runloop at line 666, avg 2µs/call # 2 times (2µs+0s) by Benchmark::timeit at line 697, avg 950ns/call # 2 times (2µs+0s) by Benchmark::timeit at line 698, avg 850ns/call # 2 times (2µs+0s) by Benchmark::timeit at line 699, avg 750ns/call
sub timedebug {
618102µs my($msg, $t) = @_;
6191017µs print STDERR "$msg",timestr($t),"\n" if $Debug;
620}
621
622# --- Functions implementing low-level support for timing loops
623
6241700ns$_Usage{runloop} = <<'USAGE';
625usage: runloop($number, [$string | $coderef])
626USAGE
627
628
# spent 6.68s (6.01ms+6.67) within Benchmark::runloop which was called 4 times, avg 1.67s/call: # 2 times (2.97ms+6.39s) by Benchmark::timeit at line 694, avg 3.19s/call # 2 times (3.04ms+289ms) by Benchmark::timeit at line 687, avg 146ms/call
sub runloop {
62941µs my($n, $c) = @_;
630
63141µs $n+=0; # force numeric now, so garbage won't creep into the eval
6324800ns croak "negative loopcount $n" if $n<0;
6334600ns confess usage unless defined $c;
6344800ns my($t0, $t1, $td); # before, after, difference
635
636 # find package of caller so we can execute code there
637410µs my($curpack) = caller(0);
63841µs my($i, $pack)= 0;
639411µs while (($pack) = caller(++$i)) {
6401658µs last if $pack ne $curpack;
641 }
642
6434400ns my ($subcode, $subref);
64443µs if (ref $c eq 'CODE') {
64544µs $subcode = "sub { for (1 .. $n) { local \$_; package $pack; &\$c; } }";
6464237µs $subref = eval $subcode;
# spent 304ms executing statements in 4 string evals (merged)
# includes 759ms spent executing 4 calls to 1 sub defined therein.
647 }
648 else {
649 $subcode = "sub { for (1 .. $n) { local \$_; package $pack; $c;} }";
650 $subref = _doeval($subcode);
651 }
6524700ns croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@;
6534400ns print STDERR "runloop $n '$subcode'\n" if $Debug;
654
655 # Wait for the user timer to tick. This makes the error range more like
656 # -0.01, +0. If we don't wait, then it's more like -0.01, +0.01. This
657 # may not seem important, but it significantly reduces the chances of
658 # getting a too low initial $n in the initial, 'find the minimum' loop
659 # in &countit. This, in turn, can reduce the number of calls to
660 # &runloop a lot, and thus reduce additive errors.
661417µs441µs my $tbase = Benchmark->new(0)->[1];
# spent 41µs making 4 calls to Benchmark::new, avg 10µs/call
66243.43ms292212.2ms while ( ( $t0 = Benchmark->new(0) )->[1] == $tbase ) {} ;
# spent 12.2ms making 2922 calls to Benchmark::new, avg 4µs/call
66344µs46.66s $subref->();
# spent 6.66s making 4 calls to Benchmark::__ANON__[(eval 333)[Benchmark.pm:646]:1], avg 1.67s/call
664411µs440µs $t1 = Benchmark->new($n);
# spent 40µs making 4 calls to Benchmark::new, avg 10µs/call
66546µs435µs $td = &timediff($t1, $t0);
# spent 35µs making 4 calls to Benchmark::timediff, avg 9µs/call
66645µs47µs timedebug("runloop:",$td);
# spent 7µs making 4 calls to Benchmark::timedebug, avg 2µs/call
667444µs $td;
668}
669
6701200ns$_Usage{timeit} = <<'USAGE';
671usage: $result = timeit($count, 'code' ); or
672 $result = timeit($count, sub { code } );
673USAGE
674
675
# spent 6.68s (52µs+6.68) within Benchmark::timeit which was called 2 times, avg 3.34s/call: # 2 times (52µs+6.68s) by Benchmark::timethis at line 821, avg 3.34s/call
sub timeit {
6762700ns my($n, $code) = @_;
6772600ns my($wn, $wc, $wd);
678
67921µs die usage unless defined $code and
680 (!ref $code or ref $code eq 'CODE');
681
6822700ns printf STDERR "timeit $n $code\n" if $Debug;
68322µs my $cache_key = $n . ( ref( $code ) ? 'c' : 's' );
68421µs if ($Do_Cache && exists $Cache{$cache_key} ) {
685 $wn = $Cache{$cache_key};
686 } else {
687100002175ms2292ms
# spent 40.3ms within Benchmark::__ANON__[/Users/dde/perl5/perlbrew/perls/5.18.0t/lib/5.18.0/Benchmark.pm:687] which was called 100000 times, avg 403ns/call: # 100000 times (40.3ms+0s) by Benchmark::__ANON__[(eval 333)[/Users/dde/perl5/perlbrew/perls/5.18.0t/lib/5.18.0/Benchmark.pm:646]:1] or Benchmark::__ANON__[(eval 335)[/Users/dde/perl5/perlbrew/perls/5.18.0t/lib/5.18.0/Benchmark.pm:646]:1] at line 1 of (eval 333)[Benchmark.pm:646], avg 403ns/call
$wn = &runloop($n, ref( $code ) ? sub { } : '' );
# spent 292ms making 2 calls to Benchmark::runloop, avg 146ms/call
688 # Can't let our baseline have any iterations, or they get subtracted
689 # out of the result.
69021µs $wn->[5] = 0;
69122µs $Cache{$cache_key} = $wn;
692 }
693
69422µs26.39s $wc = &runloop($n, $code);
# spent 6.39s making 2 calls to Benchmark::runloop, avg 3.19s/call
695
69622µs210µs $wd = timediff($wc, $wn);
# spent 10µs making 2 calls to Benchmark::timediff, avg 5µs/call
69722µs22µs timedebug("timeit: ",$wc);
# spent 2µs making 2 calls to Benchmark::timedebug, avg 950ns/call
69822µs22µs timedebug(" - ",$wn);
# spent 2µs making 2 calls to Benchmark::timedebug, avg 850ns/call
69921µs22µs timedebug(" = ",$wd);
# spent 2µs making 2 calls to Benchmark::timedebug, avg 750ns/call
700
70127µs $wd;
702}
703
704
7051100nsmy $default_for = 3;
7061100nsmy $min_for = 0.1;
707
708
7091200ns$_Usage{countit} = <<'USAGE';
710usage: $result = countit($time, 'code' ); or
711 $result = countit($time, sub { code } );
712USAGE
713
714sub countit {
715 my ( $tmax, $code ) = @_;
716
717 die usage unless @_;
718
719 if ( not defined $tmax or $tmax == 0 ) {
720 $tmax = $default_for;
721 } elsif ( $tmax < 0 ) {
722 $tmax = -$tmax;
723 }
724
725 die "countit($tmax, ...): timelimit cannot be less than $min_for.\n"
726 if $tmax < $min_for;
727
728 my ($n, $tc);
729
730 # First find the minimum $n that gives a significant timing.
731 my $zeros=0;
732 for ($n = 1; ; $n *= 2 ) {
733 my $td = timeit($n, $code);
734 $tc = $td->[1] + $td->[2];
735 if ( $tc <= 0 and $n > 1024 ) {
736 ++$zeros > 16
737 and die "Timing is consistently zero in estimation loop, cannot benchmark. N=$n\n";
738 } else {
739 $zeros = 0;
740 }
741 last if $tc > 0.1;
742 }
743
744 my $nmin = $n;
745
746 # Get $n high enough that we can guess the final $n with some accuracy.
747 my $tpra = 0.1 * $tmax; # Target/time practice.
748 while ( $tc < $tpra ) {
749 # The 5% fudge is to keep us from iterating again all
750 # that often (this speeds overall responsiveness when $tmax is big
751 # and we guess a little low). This does not noticably affect
752 # accuracy since we're not counting these times.
753 $n = int( $tpra * 1.05 * $n / $tc ); # Linear approximation.
754 my $td = timeit($n, $code);
755 my $new_tc = $td->[1] + $td->[2];
756 # Make sure we are making progress.
757 $tc = $new_tc > 1.2 * $tc ? $new_tc : 1.2 * $tc;
758 }
759
760 # Now, do the 'for real' timing(s), repeating until we exceed
761 # the max.
762 my $ntot = 0;
763 my $rtot = 0;
764 my $utot = 0.0;
765 my $stot = 0.0;
766 my $cutot = 0.0;
767 my $cstot = 0.0;
768 my $ttot = 0.0;
769
770 # The 5% fudge is because $n is often a few % low even for routines
771 # with stable times and avoiding extra timeit()s is nice for
772 # accuracy's sake.
773 $n = int( $n * ( 1.05 * $tmax / $tc ) );
774 $zeros=0;
775 while () {
776 my $td = timeit($n, $code);
777 $ntot += $n;
778 $rtot += $td->[0];
779 $utot += $td->[1];
780 $stot += $td->[2];
781 $cutot += $td->[3];
782 $cstot += $td->[4];
783 $ttot = $utot + $stot;
784 last if $ttot >= $tmax;
785 if ( $ttot <= 0 ) {
786 ++$zeros > 16
787 and die "Timing is consistently zero, cannot benchmark. N=$n\n";
788 } else {
789 $zeros = 0;
790 }
791 $ttot = 0.01 if $ttot < 0.01;
792 my $r = $tmax / $ttot - 1; # Linear approximation.
793 $n = int( $r * $ntot );
794 $n = $nmin if $n < $nmin;
795 }
796
797 return bless [ $rtot, $utot, $stot, $cutot, $cstot, $ntot ];
798}
799
800# --- Functions implementing high-level time-then-print utilities
801
802sub n_to_for {
803 my $n = shift;
804 return $n == 0 ? $default_for : $n < 0 ? -$n : undef;
805}
806
8071200ns$_Usage{timethis} = <<'USAGE';
808usage: $result = timethis($time, 'code' ); or
809 $result = timethis($time, sub { code } );
810USAGE
811
812
# spent 6.68s (40µs+6.68) within Benchmark::timethis which was called 2 times, avg 3.34s/call: # 2 times (40µs+6.68s) by Benchmark::timethese at line 877, avg 3.34s/call
sub timethis{
81321µs my($n, $code, $title, $style) = @_;
8142300ns my($t, $forn);
815
81622µs die usage unless defined $code and
817 (!ref $code or ref $code eq 'CODE');
818
8192900ns if ( $n > 0 ) {
8202900ns croak "non-integer loopcount $n, stopped" if int($n)<$n;
82123µs26.68s $t = timeit($n, $code);
# spent 6.68s making 2 calls to Benchmark::timeit, avg 3.34s/call
8222900ns $title = "timethis $n" unless defined $title;
823 } else {
824 my $fort = n_to_for( $n );
825 $t = countit( $fort, $code );
826 $title = "timethis for $fort" unless defined $title;
827 $forn = $t->[-1];
828 }
82928µs local $| = 1;
8302400ns $style = "" unless defined $style;
83121µs printf("%10s: ", $title) unless $style eq 'none';
8322500ns print timestr($t, $style, $Default_Format),"\n" unless $style eq 'none';
833
8342400ns $n = $forn if defined $forn;
835
836 # A conservative warning to spot very silly tests.
837 # Don't assume that your benchmark is ok simply because
838 # you don't get this warning!
83928µs410µs print " (warning: too few iterations for a reliable count)\n"
# spent 6µs making 2 calls to Benchmark::cpu_a, avg 3µs/call # spent 4µs making 2 calls to Benchmark::real, avg 2µs/call
840 if $n < $Min_Count
841 || ($t->real < 1 && $n < 1000)
842 || $t->cpu_a < $Min_CPU;
84326µs $t;
844}
845
846
84712µs$_Usage{timethese} = <<'USAGE';
848usage: timethese($count, { Name1 => 'code1', ... }); or
849 timethese($count, { Name1 => sub { code1 }, ... });
850USAGE
851
852
# spent 6.68s (26µs+6.68) within Benchmark::timethese which was called: # once (26µs+6.68s) by Benchmark::cmpthese at line 903
sub timethese{
8531500ns my($n, $alt, $style) = @_;
8541600ns die usage unless ref $alt eq 'HASH';
855
85618µs12µs my @names = sort keys %$alt;
# spent 2µs making 1 call to Benchmark::CORE:sort
8571300ns $style = "" unless defined $style;
8581500ns print "Benchmark: " unless $style eq 'none';
8591600ns if ( $n > 0 ) {
8601700ns croak "non-integer loopcount $n, stopped" if int($n)<$n;
8611300ns print "timing $n iterations of" unless $style eq 'none';
862 } else {
863 print "running" unless $style eq 'none';
864 }
8651300ns print " ", join(', ',@names) unless $style eq 'none';
8661400ns unless ( $n > 0 ) {
867 my $for = n_to_for( $n );
868 print ", each" if $n > 1 && $style ne 'none';
869 print " for at least $for CPU seconds" unless $style eq 'none';
870 }
8711300ns print "...\n" unless $style eq 'none';
872
873 # we could save the results in an array and produce a summary here
874 # sum, min, max, avg etc etc
8751200ns my %results;
8761800ns foreach my $name (@names) {
87727µs26.68s $results{$name} = timethis ($n, $alt -> {$name}, $name, $style);
# spent 6.68s making 2 calls to Benchmark::timethis, avg 3.34s/call
878 }
879
88015µs return \%results;
881}
882
883
8841400ns$_Usage{cmpthese} = <<'USAGE';
885usage: cmpthese($count, { Name1 => 'code1', ... }); or
886 cmpthese($count, { Name1 => sub { code1 }, ... }); or
887 cmpthese($result, $style);
888USAGE
889
890
# spent 6.68s (108µs+6.68) within Benchmark::cmpthese which was called: # once (108µs+6.68s) by main::RUNTIME at line 34 of examples/Atom-timer.pl
sub cmpthese{
8911200ns my ($results, $style);
892
893 # $count can be a blessed object.
8941800ns if ( ref $_[0] eq 'HASH' ) {
895 ($results, $style) = @_;
896 }
897 else {
89811µs my($count, $code) = @_[0,1];
8991400ns $style = $_[2] if defined $_[2];
900
9011600ns die usage unless ref $code eq 'HASH';
902
90312µs16.68s $results = timethese($count, $code, ($style || "none"));
# spent 6.68s making 1 call to Benchmark::timethese
904 }
905
9061600ns $style = "" unless defined $style;
907
908 # Flatten in to an array of arrays with the name as the first field
90916µs my @vals = map{ [ $_, @{$results->{$_}} ] } keys %$results;
910
91111µs for (@vals) {
912 # The epsilon fudge here is to prevent div by 0. Since clock
913 # resolutions are much larger, it's below the noise floor.
9142400ns my $elapsed = do {
9152900ns if ($style eq 'nop') {$_->[4]+$_->[5]}
91622µs elsif ($style eq 'noc') {$_->[2]+$_->[3]}
917 else {$_->[2]+$_->[3]+$_->[4]+$_->[5]}
918 };
91921µs my $rate = $_->[6]/(($elapsed)+0.000000000000001);
92021µs $_->[7] = $rate;
921 }
922
923 # Sort by rate
92417µs14µs @vals = sort { $a->[7] <=> $b->[7] } @vals;
# spent 4µs making 1 call to Benchmark::CORE:sort
925
926 # If more than half of the rates are greater than one...
92712µs my $display_as_rate = @vals ? ($vals[$#vals>>1]->[7] > 1) : 0;
928
9291100ns my @rows;
9301100ns my @col_widths;
931
932 my @top_row = (
933 '',
934 $display_as_rate ? 'Rate' : 's/iter',
93512µs map { $_->[0] } @vals
936 );
937
9381500ns push @rows, \@top_row;
93912µs @col_widths = map { length( $_ ) } @top_row;
940
941 # Build the data rows
942 # We leave the last column in even though it never has any data. Perhaps
943 # it should go away. Also, perhaps a style for a single column of
944 # percentages might be nice.
9451800ns for my $row_val ( @vals ) {
9462200ns my @row;
947
948 # Column 0 = test name
9492800ns push @row, $row_val->[0];
9502900ns $col_widths[0] = length( $row_val->[0] )
951 if length( $row_val->[0] ) > $col_widths[0];
952
953 # Column 1 = performance
9542400ns my $row_rate = $row_val->[7];
955
956 # We assume that we'll never get a 0 rate.
9572500ns my $rate = $display_as_rate ? $row_rate : 1 / $row_rate;
958
959 # Only give a few decimal places before switching to sci. notation,
960 # since the results aren't usually that accurate anyway.
9612900ns my $format =
962 $rate >= 100 ?
963 "%0.0f" :
964 $rate >= 10 ?
965 "%0.1f" :
966 $rate >= 1 ?
967 "%0.2f" :
968 $rate >= 0.1 ?
969 "%0.3f" :
970 "%0.2e";
971
9722900ns $format .= "/s"
973 if $display_as_rate;
974
97526µs my $formatted_rate = sprintf( $format, $rate );
9762500ns push @row, $formatted_rate;
9772700ns $col_widths[1] = length( $formatted_rate )
978 if length( $formatted_rate ) > $col_widths[1];
979
980 # Columns 2..N = performance ratios
9812200ns my $skip_rest = 0;
98221µs for ( my $col_num = 0 ; $col_num < @vals ; ++$col_num ) {
9834400ns my $col_val = $vals[$col_num];
9844100ns my $out;
98542µs if ( $skip_rest ) {
986 $out = '';
987 }
988 elsif ( $col_val->[0] eq $row_val->[0] ) {
989 $out = "--";
990 # $skip_rest = 1;
991 }
992 else {
9932300ns my $col_rate = $col_val->[7];
99423µs $out = sprintf( "%.0f%%", 100*$row_rate/$col_rate - 100 );
995 }
9964800ns push @row, $out;
99741µs $col_widths[$col_num+2] = length( $out )
998 if length( $out ) > $col_widths[$col_num+2];
999
1000 # A little wierdness to set the first column width properly
100141µs $col_widths[$col_num+2] = length( $col_val->[0] )
1002 if length( $col_val->[0] ) > $col_widths[$col_num+2];
1003 }
100421µs push @rows, \@row;
1005 }
1006
10071300ns return \@rows if $style eq "none";
1008
1009 # Equalize column widths in the chart as much as possible without
1010 # exceeding 80 characters. This does not use or affect cols 0 or 1.
1011 my @sorted_width_refs =
101237µs11µs sort { $$a <=> $$b } map { \$_ } @col_widths[2..$#col_widths];
# spent 1µs making 1 call to Benchmark::CORE:sort
10131500ns my $max_width = ${$sorted_width_refs[-1]};
1014
10151400ns my $total = @col_widths - 1 ;
101651µs for ( @col_widths ) { $total += $_ }
1017
1018 STRETCHER:
10191400ns while ( $total < 80 ) {
10201200ns my $min_width = ${$sorted_width_refs[0]};
1021 last
10221800ns if $min_width == $max_width;
1023 for ( @sorted_width_refs ) {
1024 last
1025 if $$_ > $min_width;
1026 ++$$_;
1027 ++$total;
1028 last STRETCHER
1029 if $total >= 80;
1030 }
1031 }
1032
1033 # Dump the output
103415µs my $format = join( ' ', map { "%${_}s" } @col_widths ) . "\n";
103511µs substr( $format, 1, 0 ) = '-';
10361900ns for ( @rows ) {
1037336µs317µs printf $format, @$_;
# spent 17µs making 3 calls to Benchmark::CORE:prtf, avg 6µs/call
1038 }
1039
104017µs return \@rows ;
1041}
1042
1043
1044111µs1;
 
# spent 17µs within Benchmark::CORE:prtf which was called 3 times, avg 6µs/call: # 3 times (17µs+0s) by Benchmark::cmpthese at line 1037, avg 6µs/call
sub Benchmark::CORE:prtf; # opcode
# spent 7µs within Benchmark::CORE:sort which was called 3 times, avg 2µs/call: # once (4µs+0s) by Benchmark::cmpthese at line 924 # once (2µs+0s) by Benchmark::timethese at line 856 # once (1µs+0s) by Benchmark::cmpthese at line 1012
sub Benchmark::CORE:sort; # opcode