This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate macperl patch #16868.
[perl5.git] / lib / Benchmark.pm
index b39ec46..cda764f 100644 (file)
@@ -6,6 +6,8 @@ Benchmark - benchmark running times of Perl code
 
 =head1 SYNOPSIS
 
+    use Benchmark qw(:all) ;
+
     timethis ($count, "code");
 
     # Use Perl code in strings...
@@ -387,6 +389,12 @@ September, 1999; by Barrie Slaymaker: math fixes and accuracy and
 efficiency tweaks.  Added cmpthese().  A result is now returned from 
 timethese().  Exposed countit() (was runfor()).
 
+December, 2001; by Nicholas Clark: make timestr() recognise the style 'none'
+and return an empty string. If cmpthese is calling timethese, make it pass the
+style in. (so that 'none' will suppress output). Make sub new dump its
+debugging output to STDERR, to be consistent with everything else.
+All bugs found while writing a regression test.
+
 =cut
 
 # evaluate something in a clean lexical environment
@@ -402,8 +410,9 @@ use Exporter;
 @EXPORT=qw(timeit timethis timethese timediff timestr);
 @EXPORT_OK=qw(timesum cmpthese countit
              clearcache clearallcache disablecache enablecache);
+%EXPORT_TAGS=( all => [ @EXPORT, @EXPORT_OK ] ) ;
 
-$VERSION = 1.01;
+$VERSION = 1.04;
 
 &init;
 
@@ -432,7 +441,7 @@ sub disablecache  { $cache = 0; }
 # --- Functions to process the 'time' data type
 
 sub new { my @t = (time, times, @_ == 2 ? $_[1] : 0);
-         print "new=@t\n" if $debug;
+         print STDERR "new=@t\n" if $debug;
          bless \@t; }
 
 sub cpu_p { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $pu+$ps         ; }
@@ -468,6 +477,7 @@ sub timestr {
     $f = $defaultfmt unless defined $f;
     # format a time in the required style, other formats may be added here
     $style ||= $defaultstyle;
+    return '' if $style eq 'none';
     $style = ($ct>0) ? 'all' : 'noc' if $style eq 'auto';
     my $s = "@t $style"; # default for unknown style
     $s=sprintf("%2d wallclock secs (%$f usr %$f sys + %$f cusr %$f csys = %$f CPU)",
@@ -539,7 +549,7 @@ sub timeit {
     if ($cache && exists $cache{$cache_key} ) {
        $wn = $cache{$cache_key};
     } else {
-       $wn = &runloop($n, ref( $code ) ? sub { undef } : '' );
+       $wn = &runloop($n, ref( $code ) ? sub { } : '' );
        # Can't let our baseline have any iterations, or they get subtracted
        # out of the result.
        $wn->[5] = 0;
@@ -687,7 +697,8 @@ sub timethese{
     print " ", join(', ',@names) unless $style eq 'none';
     unless ( $n > 0 ) {
        my $for = n_to_for( $n );
-       print ", each for at least $for CPU seconds" unless $style eq 'none';
+       print ", each" if $n > 1 && $style ne 'none';
+       print " for at least $for CPU seconds" unless $style eq 'none';
     }
     print "...\n" unless $style eq 'none';
 
@@ -702,7 +713,7 @@ sub timethese{
 }
 
 sub cmpthese{
-    my ($results, $style) = ref $_[0] ? @_ : ( timethese( @_[0,1] ), $_[2] ) ;
+    my ($results, $style) = ref $_[0] ? @_ : ( timethese( @_[0,1,2] ), $_[2] ) ;
 
     $style = "" unless defined $style;