speed.t revision 7c478bd95313f5f23a4c958a745db2134aa03244
use lib '..';
use Memoize;
if (-e '.fast') {
print "1..0\n";
exit 0;
}
$| = 1;
# If we don't say anything, maybe nobody will notice.
# print STDERR "\nWarning: I'm testing the speedup. This might take up to thirty seconds.\n ";
my $COARSE_TIME = 1;
} else {
*my_time = \×_to_time;
}
print "1..6\n";
# This next test finds an example that takes a long time to run, then
# checks to make sure that the run is actually speeded up by memoization.
# In some sense, this is the most essential correctness test in the package.
#
# We do this by running the fib() function with successfily larger
# arguments until we find one that tales at least $LONG_RUN seconds
# to execute. Then we memoize fib() and run the same call cagain. If
# it doesn't produce the same test in less than one-tenth the time,
# something is seriously wrong.
#
# $LONG_RUN is the number of seconds that the function call must last
# in order for the call to be considered sufficiently long.
sub fib {
my $n = shift;
$COUNT++;
return $n if $n < 2;
}
sub max { $_[0] > $_[1] ?
$_[0] : $_[1]
}
$N = 1;
$ELAPSED = 0;
my $LONG_RUN = 10;
while (1) {
my $start = time;
$COUNT=0;
# we'd expect that fib(n+1) takes about 1.618 times as long as fib(n)
# so now that we have a longish run, let's estimate the value of $N
# that will get us a sufficiently long run.
print "# OK, N=$N ought to do it.\n";
# It's important not to overshoot here because the running time
# is exponential in $N. If we increase $N too aggressively,
# the user will be forced to wait a very long time.
} else {
$N++;
}
}
$COUNT=0;
$start = time;
# If it's not ten times as fast, something is seriously wrong.
# If it called the function more than $N times, it wasn't memoized properly
# Do it again. Should be even faster this time.
$COUNT = 0;
$start = time;
# This time it shouldn't have called the function at all.