$n = $forn if defined $forn;
+ if ($t->elapsed($style) < 0) {
+ # due to clock granularity and variable CPU speed and load,
+ # on quick code with a small number of loops, it's possible for
+ # the empty loop to appear to take longer than the real loop
+ # (e.g. 1 tick verses 0 ticks). This leads to a negative elapsed
+ # time. In this case, floor it at zero, to stop bizarre results.
+ print " (warning: too few iterations for a reliable count)\n";
+ $t->[$_] = 0 for 1..4;
+ }
+
# A conservative warning to spot very silly tests.
# Don't assume that your benchmark is ok simply because
# you don't get this warning!
my $out = tie *OUT, 'TieOut';
-my $iterations = 3;
+my $iterations = 100;
$foo = 0;
select(OUT);
{
$foo = $bar = 0;
select(OUT);
- my $chart = cmpthese( 10, $code_to_test, 'nop' ) ;
+ my $chart = cmpthese($iterations, $code_to_test, 'nop' ) ;
select(STDOUT);
ok ($foo > 0, "Foo code was run");
ok ($bar > 0, "Bar code was run");
$got = $out->read();
# Remove any warnings about having too few iterations.
$got =~ s/\(warning:[^\)]+\)//gs;
- like ($got, qr/timing 10 iterations of\s+Bar\W+Foo\W*?\.\.\./s,
+ like ($got, qr/timing $iterations iterations of\s+Bar\W+Foo\W*?\.\.\./s,
'check title');
# Remove the title
$got =~ s/.*\.\.\.//s;
{
$foo = $bar = 0;
select(OUT);
- my $chart = cmpthese( 10, $code_to_test, 'none' ) ;
+ my $chart = cmpthese($iterations, $code_to_test, 'none' ) ;
select(STDOUT);
ok ($foo > 0, "Foo code was run");
ok ($bar > 0, "Bar code was run");
check_graph (@$chart);
}
+# this is a repeat of the above test, but with the timing and charting
+# steps split.
+
{
$foo = $bar = 0;
select(OUT);
- my $res = timethese( 10, $code_to_test, 'none' ) ;
+ my $res = timethese($iterations, $code_to_test, 'none' ) ;
my $chart = cmpthese($res, 'none' ) ;
select(STDOUT);
ok ($foo > 0, "Foo code was run");