Skip to content

Commit

Permalink
Merge 824df2b into 5fe711c
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeenan committed Sep 18, 2021
2 parents 5fe711c + 824df2b commit 3b1fbf4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/Benchmark.pm
Expand Up @@ -699,7 +699,7 @@ sub runloop {
$subref = _doeval($subcode);
}
croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@;
print STDERR "runloop $n '$subcode'\n" if $Debug;
print STDERR "XXX1: runloop $n '$subcode'\n" if $Debug;

# Wait for the user timer to tick. This makes the error range more like
# -0.01, +0. If we don't wait, then it's more like -0.01, +0.01. This
Expand All @@ -719,10 +719,13 @@ sub runloop {
for (my $i=0; $i < $limit; $i++) { my $x = $i / 1.5 } # burn user CPU
$limit *= 1.1;
}
print STDERR "XXX2:\n" if $Debug;
$subref->();
print STDERR "XXX3:\n" if $Debug;
$t1 = Benchmark->new($n);
$td = &timediff($t1, $t0);
timedebug("runloop:",$td);
print STDERR "XXX4:\n" if $Debug;
$td;
}

Expand All @@ -741,15 +744,19 @@ sub timeit {
printf STDERR "timeit $n $code\n" if $Debug;
my $cache_key = $n . ( ref( $code ) ? 'c' : 's' );
if ($Do_Cache && exists $Cache{$cache_key} ) {
$wn = $Cache{$cache_key};
} else {
$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;
$Cache{$cache_key} = $wn;
print STDERR "CCC: Why do I think I have a cache?\n" if $Debug;
$wn = $Cache{$cache_key};
}
else {
print STDERR "AAA: About to call runloop for 'wn'\n" if $Debug;
$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;
$Cache{$cache_key} = $wn;
}

print STDERR "BBB: About to call runloop for 'wc'\n" if $Debug;
$wc = &runloop($n, $code);

$wd = timediff($wc, $wn);
Expand Down
23 changes: 23 additions & 0 deletions lib/yBenchmark.t
@@ -0,0 +1,23 @@
#!./perl -w
# https://rt.perl.org/Ticket/Display.html?id=131531
BEGIN {
chdir 't' if -d 't';
@INC = ('../lib');
}

use warnings;
use strict;

use Benchmark qw(:all);
$Benchmark::Debug++;

my $foo = 0;
my $reps = 5;
my $coderef = sub {++$foo};
my $t = timeit($reps, $coderef);
($t->isa('Benchmark'))
? print "timeit CODEREF returned a Benchmark object\n"
: print "timeit CODEREF did NOT return a Benchmark object\n";
($foo == $reps)
? print "benchmarked code was run $reps times\n"
: print "FAIL: requested $reps reps, but $foo were run\n";

0 comments on commit 3b1fbf4

Please sign in to comment.