functionality.
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.
September, 2002; by Jarkko Hietaniemi: add ':hireswallclock' special tag.
February, 2004; by Chia-liang Kao: make cmpthese and timestr use time
statistics for children instead of parent when the style is 'nop'.
=cut
# evaluate something in a clean lexical environment
sub _doeval { no strict; eval shift }
#
# put any lexicals at file scope AFTER here
#
use Carp;
use Exporter;
our(@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
@ISA=qw(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.07;
# --- ':hireswallclock' special handling
my $hirestime;
sub mytime () { time }
init();
sub BEGIN {
if (eval 'require Time::HiRes') {
import Time::HiRes qw(time);
$hirestime = \&Time::HiRes::time;
}
}
sub import {
my $class = shift;
if (grep { $_ eq ":hireswallclock" } @_) {
@_ = grep { $_ ne ":hireswallclock" } @_;
*mytime = $hirestime if defined $hirestime;
}
Benchmark->export_to_level(1, $class, @_);
}
our($Debug, $Min_Count, $Min_CPU, $Default_Format, $Default_Style,
%_Usage, %Cache, $Do_Cache);
sub init {
$Debug = 0;
$Min_Count = 4;
$Min_CPU = 0.4;
$Default_Format = '5.2f';
$Default_Style = 'auto';
# The cache can cause a slight loss of sys time accuracy. If a
# user does many tests (>10) with *very* large counts (>10000)
# or works on a very slow machine the cache may be useful.
disablecache();
clearallcache();
}
sub debug { $Debug = ($_[1] != 0); }
sub usage {
my $calling_sub = (caller(1))[3];
$calling_sub =~ s/^Benchmark:://;
return $_Usage{$calling_sub} || '';
}
# The cache needs two branches: 's' for strings and 'c' for code. The
# empty loop is different in these two cases.
$_Usage{clearcache} = <<'USAGE';
usage: clearcache($count);
USAGE
sub clearcache {
die usage unless @_ == 1;
delete $Cache{"$_[0]c"}; delete $Cache{"$_[0]s"};
}
$_Usage{clearallcache} = <<'USAGE';
=5= |