Allow several arguments to display().
[p5sagit/p5-mst-13.2.git] / lib / Benchmark.pm
1 package Benchmark;
2
3 =head1 NAME
4
5 Benchmark - benchmark running times of Perl code
6
7 =head1 SYNOPSIS
8
9     use Benchmark qw(:all) ;
10
11     timethis ($count, "code");
12
13     # Use Perl code in strings...
14     timethese($count, {
15         'Name1' => '...code1...',
16         'Name2' => '...code2...',
17     });
18
19     # ... or use subroutine references.
20     timethese($count, {
21         'Name1' => sub { ...code1... },
22         'Name2' => sub { ...code2... },
23     });
24
25     # cmpthese can be used both ways as well
26     cmpthese($count, {
27         'Name1' => '...code1...',
28         'Name2' => '...code2...',
29     });
30
31     cmpthese($count, {
32         'Name1' => sub { ...code1... },
33         'Name2' => sub { ...code2... },
34     });
35
36     # ...or in two stages
37     $results = timethese($count, 
38         {
39             'Name1' => sub { ...code1... },
40             'Name2' => sub { ...code2... },
41         },
42         'none'
43     );
44     cmpthese( $results ) ;
45
46     $t = timeit($count, '...other code...')
47     print "$count loops of other code took:",timestr($t),"\n";
48
49     $t = countit($time, '...other code...')
50     $count = $t->iters ;
51     print "$count loops of other code took:",timestr($t),"\n";
52
53 =head1 DESCRIPTION
54
55 The Benchmark module encapsulates a number of routines to help you
56 figure out how long it takes to execute some code.
57
58 timethis - run a chunk of code several times
59
60 timethese - run several chunks of code several times
61
62 cmpthese - print results of timethese as a comparison chart
63
64 timeit - run a chunk of code and see how long it goes
65
66 countit - see how many times a chunk of code runs in a given time
67
68
69 =head2 Methods
70
71 =over 10
72
73 =item new
74
75 Returns the current time.   Example:
76
77     use Benchmark;
78     $t0 = new Benchmark;
79     # ... your code here ...
80     $t1 = new Benchmark;
81     $td = timediff($t1, $t0);
82     print "the code took:",timestr($td),"\n";
83
84 =item debug
85
86 Enables or disable debugging by setting the C<$Benchmark::Debug> flag:
87
88     debug Benchmark 1;
89     $t = timeit(10, ' 5 ** $Global ');
90     debug Benchmark 0;
91
92 =item iters
93
94 Returns the number of iterations.
95
96 =back
97
98 =head2 Standard Exports
99
100 The following routines will be exported into your namespace
101 if you use the Benchmark module:
102
103 =over 10
104
105 =item timeit(COUNT, CODE)
106
107 Arguments: COUNT is the number of times to run the loop, and CODE is
108 the code to run.  CODE may be either a code reference or a string to
109 be eval'd; either way it will be run in the caller's package.
110
111 Returns: a Benchmark object.
112
113 =item timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] )
114
115 Time COUNT iterations of CODE. CODE may be a string to eval or a
116 code reference; either way the CODE will run in the caller's package.
117 Results will be printed to STDOUT as TITLE followed by the times.
118 TITLE defaults to "timethis COUNT" if none is provided. STYLE
119 determines the format of the output, as described for timestr() below.
120
121 The COUNT can be zero or negative: this means the I<minimum number of
122 CPU seconds> to run.  A zero signifies the default of 3 seconds.  For
123 example to run at least for 10 seconds:
124
125         timethis(-10, $code)
126
127 or to run two pieces of code tests for at least 3 seconds:
128
129         timethese(0, { test1 => '...', test2 => '...'})
130
131 CPU seconds is, in UNIX terms, the user time plus the system time of
132 the process itself, as opposed to the real (wallclock) time and the
133 time spent by the child processes.  Less than 0.1 seconds is not
134 accepted (-0.01 as the count, for example, will cause a fatal runtime
135 exception).
136
137 Note that the CPU seconds is the B<minimum> time: CPU scheduling and
138 other operating system factors may complicate the attempt so that a
139 little bit more time is spent.  The benchmark output will, however,
140 also tell the number of C<$code> runs/second, which should be a more
141 interesting number than the actually spent seconds.
142
143 Returns a Benchmark object.
144
145 =item timethese ( COUNT, CODEHASHREF, [ STYLE ] )
146
147 The CODEHASHREF is a reference to a hash containing names as keys
148 and either a string to eval or a code reference for each value.
149 For each (KEY, VALUE) pair in the CODEHASHREF, this routine will
150 call
151
152         timethis(COUNT, VALUE, KEY, STYLE)
153
154 The routines are called in string comparison order of KEY.
155
156 The COUNT can be zero or negative, see timethis().
157
158 Returns a hash of Benchmark objects, keyed by name.
159
160 =item timediff ( T1, T2 )
161
162 Returns the difference between two Benchmark times as a Benchmark
163 object suitable for passing to timestr().
164
165 =item timestr ( TIMEDIFF, [ STYLE, [ FORMAT ] ] )
166
167 Returns a string that formats the times in the TIMEDIFF object in
168 the requested STYLE. TIMEDIFF is expected to be a Benchmark object
169 similar to that returned by timediff().
170
171 STYLE can be any of 'all', 'none', 'noc', 'nop' or 'auto'. 'all' shows
172 each of the 5 times available ('wallclock' time, user time, system time,
173 user time of children, and system time of children). 'noc' shows all
174 except the two children times. 'nop' shows only wallclock and the
175 two children times. 'auto' (the default) will act as 'all' unless
176 the children times are both zero, in which case it acts as 'noc'.
177 'none' prevents output.
178
179 FORMAT is the L<printf(3)>-style format specifier (without the
180 leading '%') to use to print the times. It defaults to '5.2f'.
181
182 =back
183
184 =head2 Optional Exports
185
186 The following routines will be exported into your namespace
187 if you specifically ask that they be imported:
188
189 =over 10
190
191 =item clearcache ( COUNT )
192
193 Clear the cached time for COUNT rounds of the null loop.
194
195 =item clearallcache ( )
196
197 Clear all cached times.
198
199 =item cmpthese ( COUT, CODEHASHREF, [ STYLE ] )
200
201 =item cmpthese ( RESULTSHASHREF, [ STYLE ] )
202
203 Optionally calls timethese(), then outputs comparison chart.  This:
204
205     cmpthese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
206
207 outputs a chart like:
208
209            Rate    b    a
210     b 2831802/s   -- -61%
211     a 7208959/s 155%   --
212
213 This chart is sorted from slowest to fastest, and shows the percent speed
214 difference between each pair of tests.
215
216 c<cmpthese> can also be passed the data structure that timethese() returns:
217
218     $results = timethese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
219     cmpthese( $results );
220
221 in case you want to see both sets of results.
222
223 Returns a reference to an ARRAY of rows, each row is an ARRAY of cells from the
224 above chart, including labels. This:
225
226     my $rows = cmpthese( -1, { a => '++$i', b => '$i *= 2' }, "none" );
227
228 returns a data structure like:
229
230     [
231         [ '',       'Rate',   'b',    'a' ],
232         [ 'b', '2885232/s',  '--', '-59%' ],
233         [ 'a', '7099126/s', '146%',  '--' ],
234     ]
235
236 B<NOTE>: This result value differs from previous versions, which returned
237 the C<timethese()> result structure.  If you want that, just use the two
238 statement C<timethese>...C<cmpthese> idiom shown above.
239
240 Incidently, note the variance in the result values between the two examples;
241 this is typical of benchmarking.  If this were a real benchmark, you would
242 probably want to run a lot more iterations.
243
244 =item countit(TIME, CODE)
245
246 Arguments: TIME is the minimum length of time to run CODE for, and CODE is
247 the code to run.  CODE may be either a code reference or a string to
248 be eval'd; either way it will be run in the caller's package.
249
250 TIME is I<not> negative.  countit() will run the loop many times to
251 calculate the speed of CODE before running it for TIME.  The actual
252 time run for will usually be greater than TIME due to system clock
253 resolution, so it's best to look at the number of iterations divided
254 by the times that you are concerned with, not just the iterations.
255
256 Returns: a Benchmark object.
257
258 =item disablecache ( )
259
260 Disable caching of timings for the null loop. This will force Benchmark
261 to recalculate these timings for each new piece of code timed.
262
263 =item enablecache ( )
264
265 Enable caching of timings for the null loop. The time taken for COUNT
266 rounds of the null loop will be calculated only once for each
267 different COUNT used.
268
269 =item timesum ( T1, T2 )
270
271 Returns the sum of two Benchmark times as a Benchmark object suitable
272 for passing to timestr().
273
274 =back
275
276 =head1 NOTES
277
278 The data is stored as a list of values from the time and times
279 functions:
280
281       ($real, $user, $system, $children_user, $children_system, $iters)
282
283 in seconds for the whole loop (not divided by the number of rounds).
284
285 The timing is done using time(3) and times(3).
286
287 Code is executed in the caller's package.
288
289 The time of the null loop (a loop with the same
290 number of rounds but empty loop body) is subtracted
291 from the time of the real loop.
292
293 The null loop times can be cached, the key being the
294 number of rounds. The caching can be controlled using
295 calls like these:
296
297     clearcache($key);
298     clearallcache();
299
300     disablecache();
301     enablecache();
302
303 Caching is off by default, as it can (usually slightly) decrease
304 accuracy and does not usually noticably affect runtimes.
305
306 =head1 EXAMPLES
307
308 For example,
309
310     use Benchmark qw( cmpthese ) ;
311     $x = 3;
312     cmpthese( -5, {
313         a => sub{$x*$x},
314         b => sub{$x**2},
315     } );
316
317 outputs something like this:
318
319    Benchmark: running a, b, each for at least 5 CPU seconds...
320           Rate    b    a
321    b 1559428/s   -- -62%
322    a 4152037/s 166%   --
323
324
325 while 
326
327     use Benchmark qw( timethese cmpthese ) ;
328     $x = 3;
329     $r = timethese( -5, {
330         a => sub{$x*$x},
331         b => sub{$x**2},
332     } );
333     cmpthese $r;
334
335 outputs something like this:
336
337     Benchmark: running a, b, each for at least 5 CPU seconds...
338              a: 10 wallclock secs ( 5.14 usr +  0.13 sys =  5.27 CPU) @ 3835055.60/s (n=20210743)
339              b:  5 wallclock secs ( 5.41 usr +  0.00 sys =  5.41 CPU) @ 1574944.92/s (n=8520452)
340            Rate    b    a
341     b 1574945/s   -- -59%
342     a 3835056/s 144%   --
343
344
345 =head1 INHERITANCE
346
347 Benchmark inherits from no other class, except of course
348 for Exporter.
349
350 =head1 CAVEATS
351
352 Comparing eval'd strings with code references will give you
353 inaccurate results: a code reference will show a slightly slower
354 execution time than the equivalent eval'd string.
355
356 The real time timing is done using time(2) and
357 the granularity is therefore only one second.
358
359 Short tests may produce negative figures because perl
360 can appear to take longer to execute the empty loop
361 than a short test; try:
362
363     timethis(100,'1');
364
365 The system time of the null loop might be slightly
366 more than the system time of the loop with the actual
367 code and therefore the difference might end up being E<lt> 0.
368
369 =head1 SEE ALSO
370
371 L<Devel::DProf> - a Perl code profiler
372
373 =head1 AUTHORS
374
375 Jarkko Hietaniemi <F<jhi@iki.fi>>, Tim Bunce <F<Tim.Bunce@ig.co.uk>>
376
377 =head1 MODIFICATION HISTORY
378
379 September 8th, 1994; by Tim Bunce.
380
381 March 28th, 1997; by Hugo van der Sanden: added support for code
382 references and the already documented 'debug' method; revamped
383 documentation.
384
385 April 04-07th, 1997: by Jarkko Hietaniemi, added the run-for-some-time
386 functionality.
387
388 September, 1999; by Barrie Slaymaker: math fixes and accuracy and 
389 efficiency tweaks.  Added cmpthese().  A result is now returned from 
390 timethese().  Exposed countit() (was runfor()).
391
392 =cut
393
394 # evaluate something in a clean lexical environment
395 sub _doeval { eval shift }
396
397 #
398 # put any lexicals at file scope AFTER here
399 #
400
401 use Carp;
402 use Exporter;
403 @ISA=(Exporter);
404 @EXPORT=qw(timeit timethis timethese timediff timestr);
405 @EXPORT_OK=qw(timesum cmpthese countit
406               clearcache clearallcache disablecache enablecache);
407 %EXPORT_TAGS=( all => [ @EXPORT, @EXPORT_OK ] ) ;
408
409 $VERSION = 1.03;
410
411 &init;
412
413 sub init {
414     $debug = 0;
415     $min_count = 4;
416     $min_cpu   = 0.4;
417     $defaultfmt = '5.2f';
418     $defaultstyle = 'auto';
419     # The cache can cause a slight loss of sys time accuracy. If a
420     # user does many tests (>10) with *very* large counts (>10000)
421     # or works on a very slow machine the cache may be useful.
422     &disablecache;
423     &clearallcache;
424 }
425
426 sub debug { $debug = ($_[1] != 0); }
427
428 # The cache needs two branches: 's' for strings and 'c' for code.  The
429 # emtpy loop is different in these two cases.
430 sub clearcache    { delete $cache{"$_[0]c"}; delete $cache{"$_[0]s"}; }
431 sub clearallcache { %cache = (); }
432 sub enablecache   { $cache = 1; }
433 sub disablecache  { $cache = 0; }
434
435 # --- Functions to process the 'time' data type
436
437 sub new { my @t = (time, times, @_ == 2 ? $_[1] : 0);
438           print "new=@t\n" if $debug;
439           bless \@t; }
440
441 sub cpu_p { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps         ; }
442 sub cpu_c { my($r,$pu,$ps,$cu,$cs) = @{$_[0]};         $cu+$cs ; }
443 sub cpu_a { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps+$cu+$cs ; }
444 sub real  { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $r              ; }
445 sub iters { $_[0]->[5] ; }
446
447 sub timediff {
448     my($a, $b) = @_;
449     my @r;
450     for (my $i=0; $i < @$a; ++$i) {
451         push(@r, $a->[$i] - $b->[$i]);
452     }
453     bless \@r;
454 }
455
456 sub timesum {
457      my($a, $b) = @_;
458      my @r;
459      for (my $i=0; $i < @$a; ++$i) {
460         push(@r, $a->[$i] + $b->[$i]);
461      }
462      bless \@r;
463 }
464
465 sub timestr {
466     my($tr, $style, $f) = @_;
467     my @t = @$tr;
468     warn "bad time value (@t)" unless @t==6;
469     my($r, $pu, $ps, $cu, $cs, $n) = @t;
470     my($pt, $ct, $tt) = ($tr->cpu_p, $tr->cpu_c, $tr->cpu_a);
471     $f = $defaultfmt unless defined $f;
472     # format a time in the required style, other formats may be added here
473     $style ||= $defaultstyle;
474     $style = ($ct>0) ? 'all' : 'noc' if $style eq 'auto';
475     my $s = "@t $style"; # default for unknown style
476     $s=sprintf("%2d wallclock secs (%$f usr %$f sys + %$f cusr %$f csys = %$f CPU)",
477                             $r,$pu,$ps,$cu,$cs,$tt) if $style eq 'all';
478     $s=sprintf("%2d wallclock secs (%$f usr + %$f sys = %$f CPU)",
479                             $r,$pu,$ps,$pt) if $style eq 'noc';
480     $s=sprintf("%2d wallclock secs (%$f cusr + %$f csys = %$f CPU)",
481                             $r,$cu,$cs,$ct) if $style eq 'nop';
482     $s .= sprintf(" @ %$f/s (n=$n)", $n / ( $pu + $ps )) if $n && $pu+$ps;
483     $s;
484 }
485
486 sub timedebug {
487     my($msg, $t) = @_;
488     print STDERR "$msg",timestr($t),"\n" if $debug;
489 }
490
491 # --- Functions implementing low-level support for timing loops
492
493 sub runloop {
494     my($n, $c) = @_;
495
496     $n+=0; # force numeric now, so garbage won't creep into the eval
497     croak "negative loopcount $n" if $n<0;
498     confess "Usage: runloop(number, [string | coderef])" unless defined $c;
499     my($t0, $t1, $td); # before, after, difference
500
501     # find package of caller so we can execute code there
502     my($curpack) = caller(0);
503     my($i, $pack)= 0;
504     while (($pack) = caller(++$i)) {
505         last if $pack ne $curpack;
506     }
507
508     my ($subcode, $subref);
509     if (ref $c eq 'CODE') {
510         $subcode = "sub { for (1 .. $n) { local \$_; package $pack; &\$c; } }";
511         $subref  = eval $subcode;
512     }
513     else {
514         $subcode = "sub { for (1 .. $n) { local \$_; package $pack; $c;} }";
515         $subref  = _doeval($subcode);
516     }
517     croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@;
518     print STDERR "runloop $n '$subcode'\n" if $debug;
519
520     # Wait for the user timer to tick.  This makes the error range more like 
521     # -0.01, +0.  If we don't wait, then it's more like -0.01, +0.01.  This
522     # may not seem important, but it significantly reduces the chances of
523     # getting a too low initial $n in the initial, 'find the minimum' loop
524     # in &countit.  This, in turn, can reduce the number of calls to
525     # &runloop a lot, and thus reduce additive errors.
526     my $tbase = Benchmark->new(0)->[1];
527     while ( ( $t0 = Benchmark->new(0) )->[1] == $tbase ) {} ;
528     &$subref;
529     $t1 = Benchmark->new($n);
530     $td = &timediff($t1, $t0);
531     timedebug("runloop:",$td);
532     $td;
533 }
534
535
536 sub timeit {
537     my($n, $code) = @_;
538     my($wn, $wc, $wd);
539
540     printf STDERR "timeit $n $code\n" if $debug;
541     my $cache_key = $n . ( ref( $code ) ? 'c' : 's' );
542     if ($cache && exists $cache{$cache_key} ) {
543         $wn = $cache{$cache_key};
544     } else {
545         $wn = &runloop($n, ref( $code ) ? sub { undef } : '' );
546         # Can't let our baseline have any iterations, or they get subtracted
547         # out of the result.
548         $wn->[5] = 0;
549         $cache{$cache_key} = $wn;
550     }
551
552     $wc = &runloop($n, $code);
553
554     $wd = timediff($wc, $wn);
555     timedebug("timeit: ",$wc);
556     timedebug("      - ",$wn);
557     timedebug("      = ",$wd);
558
559     $wd;
560 }
561
562
563 my $default_for = 3;
564 my $min_for     = 0.1;
565
566
567 sub countit {
568     my ( $tmax, $code ) = @_;
569
570     if ( not defined $tmax or $tmax == 0 ) {
571         $tmax = $default_for;
572     } elsif ( $tmax < 0 ) {
573         $tmax = -$tmax;
574     }
575
576     die "countit($tmax, ...): timelimit cannot be less than $min_for.\n"
577         if $tmax < $min_for;
578
579     my ($n, $tc);
580
581     # First find the minimum $n that gives a significant timing.
582     for ($n = 1; ; $n *= 2 ) {
583         my $td = timeit($n, $code);
584         $tc = $td->[1] + $td->[2];
585         last if $tc > 0.1;
586     }
587
588     my $nmin = $n;
589
590     # Get $n high enough that we can guess the final $n with some accuracy.
591     my $tpra = 0.1 * $tmax; # Target/time practice.
592     while ( $tc < $tpra ) {
593         # The 5% fudge is to keep us from iterating again all
594         # that often (this speeds overall responsiveness when $tmax is big
595         # and we guess a little low).  This does not noticably affect 
596         # accuracy since we're not couting these times.
597         $n = int( $tpra * 1.05 * $n / $tc ); # Linear approximation.
598         my $td = timeit($n, $code);
599         my $new_tc = $td->[1] + $td->[2];
600         # Make sure we are making progress.
601         $tc = $new_tc > 1.2 * $tc ? $new_tc : 1.2 * $tc;
602     }
603
604     # Now, do the 'for real' timing(s), repeating until we exceed
605     # the max.
606     my $ntot  = 0;
607     my $rtot  = 0;
608     my $utot  = 0.0;
609     my $stot  = 0.0;
610     my $cutot = 0.0;
611     my $cstot = 0.0;
612     my $ttot  = 0.0;
613
614     # The 5% fudge is because $n is often a few % low even for routines
615     # with stable times and avoiding extra timeit()s is nice for
616     # accuracy's sake.
617     $n = int( $n * ( 1.05 * $tmax / $tc ) );
618
619     while () {
620         my $td = timeit($n, $code);
621         $ntot  += $n;
622         $rtot  += $td->[0];
623         $utot  += $td->[1];
624         $stot  += $td->[2];
625         $cutot += $td->[3];
626         $cstot += $td->[4];
627         $ttot = $utot + $stot;
628         last if $ttot >= $tmax;
629
630         $ttot = 0.01 if $ttot < 0.01;
631         my $r = $tmax / $ttot - 1; # Linear approximation.
632         $n = int( $r * $ntot );
633         $n = $nmin if $n < $nmin;
634     }
635
636     return bless [ $rtot, $utot, $stot, $cutot, $cstot, $ntot ];
637 }
638
639 # --- Functions implementing high-level time-then-print utilities
640
641 sub n_to_for {
642     my $n = shift;
643     return $n == 0 ? $default_for : $n < 0 ? -$n : undef;
644 }
645
646 sub timethis{
647     my($n, $code, $title, $style) = @_;
648     my($t, $for, $forn);
649
650     if ( $n > 0 ) {
651         croak "non-integer loopcount $n, stopped" if int($n)<$n;
652         $t = timeit($n, $code);
653         $title = "timethis $n" unless defined $title;
654     } else {
655         $fort  = n_to_for( $n );
656         $t     = countit( $fort, $code );
657         $title = "timethis for $fort" unless defined $title;
658         $forn  = $t->[-1];
659     }
660     local $| = 1;
661     $style = "" unless defined $style;
662     printf("%10s: ", $title) unless $style eq 'none';
663     print timestr($t, $style, $defaultfmt),"\n" unless $style eq 'none';
664
665     $n = $forn if defined $forn;
666
667     # A conservative warning to spot very silly tests.
668     # Don't assume that your benchmark is ok simply because
669     # you don't get this warning!
670     print "            (warning: too few iterations for a reliable count)\n"
671         if     $n < $min_count
672             || ($t->real < 1 && $n < 1000)
673             || $t->cpu_a < $min_cpu;
674     $t;
675 }
676
677 sub timethese{
678     my($n, $alt, $style) = @_;
679     die "usage: timethese(count, { 'Name1'=>'code1', ... }\n"
680                 unless ref $alt eq HASH;
681     my @names = sort keys %$alt;
682     $style = "" unless defined $style;
683     print "Benchmark: " unless $style eq 'none';
684     if ( $n > 0 ) {
685         croak "non-integer loopcount $n, stopped" if int($n)<$n;
686         print "timing $n iterations of" unless $style eq 'none';
687     } else {
688         print "running" unless $style eq 'none';
689     }
690     print " ", join(', ',@names) unless $style eq 'none';
691     unless ( $n > 0 ) {
692         my $for = n_to_for( $n );
693         print ", each" if $n > 1 && $style ne 'none';
694         print " for at least $for CPU seconds" unless $style eq 'none';
695     }
696     print "...\n" unless $style eq 'none';
697
698     # we could save the results in an array and produce a summary here
699     # sum, min, max, avg etc etc
700     my %results;
701     foreach my $name (@names) {
702         $results{$name} = timethis ($n, $alt -> {$name}, $name, $style);
703     }
704
705     return \%results;
706 }
707
708 sub cmpthese{
709     my ($results, $style) = ref $_[0] ? @_ : ( timethese( @_[0,1] ), $_[2] ) ;
710
711     $style = "" unless defined $style;
712
713     # Flatten in to an array of arrays with the name as the first field
714     my @vals = map{ [ $_, @{$results->{$_}} ] } keys %$results;
715
716     for (@vals) {
717         # The epsilon fudge here is to prevent div by 0.  Since clock
718         # resolutions are much larger, it's below the noise floor.
719         my $rate = $_->[6] / ( $_->[2] + $_->[3] + 0.000000000000001 );
720         $_->[7] = $rate;
721     }
722
723     # Sort by rate
724     @vals = sort { $a->[7] <=> $b->[7] } @vals;
725
726     # If more than half of the rates are greater than one...
727     my $display_as_rate = $vals[$#vals>>1]->[7] > 1;
728
729     my @rows;
730     my @col_widths;
731
732     my @top_row = ( 
733         '', 
734         $display_as_rate ? 'Rate' : 's/iter', 
735         map { $_->[0] } @vals 
736     );
737
738     push @rows, \@top_row;
739     @col_widths = map { length( $_ ) } @top_row;
740
741     # Build the data rows
742     # We leave the last column in even though it never has any data.  Perhaps
743     # it should go away.  Also, perhaps a style for a single column of
744     # percentages might be nice.
745     for my $row_val ( @vals ) {
746         my @row;
747
748         # Column 0 = test name
749         push @row, $row_val->[0];
750         $col_widths[0] = length( $row_val->[0] )
751             if length( $row_val->[0] ) > $col_widths[0];
752
753         # Column 1 = performance
754         my $row_rate = $row_val->[7];
755
756         # We assume that we'll never get a 0 rate.
757         my $a = $display_as_rate ? $row_rate : 1 / $row_rate;
758
759         # Only give a few decimal places before switching to sci. notation,
760         # since the results aren't usually that accurate anyway.
761         my $format = 
762            $a >= 100 ? 
763                "%0.0f" : 
764            $a >= 10 ?
765                "%0.1f" :
766            $a >= 1 ?
767                "%0.2f" :
768            $a >= 0.1 ?
769                "%0.3f" :
770                "%0.2e";
771
772         $format .= "/s"
773             if $display_as_rate;
774         # Using $b here due to optimizing bug in _58 through _61
775         my $b = sprintf( $format, $a );
776         push @row, $b;
777         $col_widths[1] = length( $b )
778             if length( $b ) > $col_widths[1];
779
780         # Columns 2..N = performance ratios
781         my $skip_rest = 0;
782         for ( my $col_num = 0 ; $col_num < @vals ; ++$col_num ) {
783             my $col_val = $vals[$col_num];
784             my $out;
785             if ( $skip_rest ) {
786                 $out = '';
787             }
788             elsif ( $col_val->[0] eq $row_val->[0] ) {
789                 $out = "--";
790                 # $skip_rest = 1;
791             }
792             else {
793                 my $col_rate = $col_val->[7];
794                 $out = sprintf( "%.0f%%", 100*$row_rate/$col_rate - 100 );
795             }
796             push @row, $out;
797             $col_widths[$col_num+2] = length( $out )
798                 if length( $out ) > $col_widths[$col_num+2];
799
800             # A little wierdness to set the first column width properly
801             $col_widths[$col_num+2] = length( $col_val->[0] )
802                 if length( $col_val->[0] ) > $col_widths[$col_num+2];
803         }
804         push @rows, \@row;
805     }
806
807     return \@rows if $style eq "none";
808
809     # Equalize column widths in the chart as much as possible without
810     # exceeding 80 characters.  This does not use or affect cols 0 or 1.
811     my @sorted_width_refs = 
812        sort { $$a <=> $$b } map { \$_ } @col_widths[2..$#col_widths];
813     my $max_width = ${$sorted_width_refs[-1]};
814
815     my $total = @col_widths - 1 ;
816     for ( @col_widths ) { $total += $_ }
817
818     STRETCHER:
819     while ( $total < 80 ) {
820         my $min_width = ${$sorted_width_refs[0]};
821         last
822            if $min_width == $max_width;
823         for ( @sorted_width_refs ) {
824             last 
825                 if $$_ > $min_width;
826             ++$$_;
827             ++$total;
828             last STRETCHER
829                 if $total >= 80;
830         }
831     }
832
833     # Dump the output
834     my $format = join( ' ', map { "%${_}s" } @col_widths ) . "\n";
835     substr( $format, 1, 0 ) = '-';
836     for ( @rows ) {
837         printf $format, @$_;
838     }
839
840     return \@rows ;
841 }
842
843
844 1;