August 1999 | Perl Hardware Store | Slide #24 |
Pure functions get faster when you memoize them.
A memoized function caches its return values
If you call it with the same arguments, it returns the cached value
{ my @fact = (1); sub factorial { my $n = shift;
return $fact[$n] if defined $fact[$n];
$fact[$n] = ($n == 0 ? 1 : $n * factorial($n-1)); } }
Next | Copyright © 1998 M-J. Dominus |