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