X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FBenchmark.pm;h=13acf869bc122d3b081523d8c0037eed76a47c7a;hb=bb9460edf8a898c5bc341b673bfeff1784c574c6;hp=c749116139fe3e76eaf172c4128f91c54b019e81;hpb=3f1bea595935e4179e5648faacfa5b549f2d7e03;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Benchmark.pm b/lib/Benchmark.pm index c749116..13acf86 100644 --- a/lib/Benchmark.pm +++ b/lib/Benchmark.pm @@ -14,8 +14,15 @@ timeit - run a chunk of code and see how long it goes timethis ($count, "code"); + # Use Perl code in strings... timethese($count, { 'Name1' => '...code1...', + 'Name2' => '...code2...', + }); + + # ... or use subroutine references. + timethese($count, { + 'Name1' => sub { ...code1... }, 'Name2' => sub { ...code2... }, }); @@ -169,6 +176,10 @@ for Exporter. =head1 CAVEATS +Comparing eval'd strings with code references will give you +inaccurate results: a code reference will show a slower +execution time than the equivalent eval'd string. + The real time timing is done using time(2) and the granularity is therefore only one second. @@ -184,8 +195,7 @@ code and therefore the difference might end up being E 0. =head1 AUTHORS -Jarkko Hietaniemi EFE, -Tim Bunce EFE +Jarkko Hietaniemi >, Tim Bunce > =head1 MODIFICATION HISTORY @@ -251,7 +261,7 @@ sub timestr { my($pt, $ct, $t) = ($tr->cpu_p, $tr->cpu_c, $tr->cpu_a); $f = $defaultfmt unless defined $f; # format a time in the required style, other formats may be added here - $style = $defaultstyle unless defined $style; + $style ||= $defaultstyle; $style = ($ct>0) ? 'all' : 'noc' if $style eq 'auto'; my $s = "@t $style"; # default for unknown style $s=sprintf("%2d secs (%$f usr %$f sys + %$f cusr %$f csys = %$f cpu)",