Query: memoize
OS: centos
Section: 3pm
Format: Original Unix Latex Style Formatted with HTML and a Horizontal Scroll Bar
Memoize(3pm) Perl Programmers Reference Guide Memoize(3pm)NAMEMemoize - Make functions faster by trading space for timeSYNOPSIS# This is the documentation for Memoize 1.02 use Memoize; memoize('slow_function'); slow_function(arguments); # Is faster than it was before This is normally all you need to know. However, many options are available: memoize(function, options...); Options include: NORMALIZER => function INSTALL => new_name SCALAR_CACHE => 'MEMORY' SCALAR_CACHE => ['HASH', \%cache_hash ] SCALAR_CACHE => 'FAULT' SCALAR_CACHE => 'MERGE' LIST_CACHE => 'MEMORY' LIST_CACHE => ['HASH', \%cache_hash ] LIST_CACHE => 'FAULT' LIST_CACHE => 'MERGE'DESCRIPTION`Memoizing' a function makes it faster by trading space for time. It does this by caching the return values of the function in a table. If you call the function again with the same arguments, "memoize" jumps in and gives you the value out of the table, instead of letting the function compute the value all over again. Here is an extreme example. Consider the Fibonacci sequence, defined by the following function: # Compute Fibonacci numbers sub fib { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); } This function is very slow. Why? To compute fib(14), it first wants to compute fib(13) and fib(12), and add the results. But to compute fib(13), it first has to compute fib(12) and fib(11), and then it comes back and computes fib(12) all over again even though the answer is the same. And both of the times that it wants to compute fib(12), it has to compute fib(11) from scratch, and then it has to do it again each time it wants to compute fib(13). This function does so much recomputing of old results that it takes a really long time to run---fib(14) makes 1,200 extra recursive calls to itself, to compute and recompute things that it already computed. This function is a good candidate for memoization. If you memoize the `fib' function above, it will compute fib(14) exactly once, the first time it needs to, and then save the result in a table. Then if you ask for fib(14) again, it gives you the result out of the table. While computing fib(14), instead of computing fib(12) twice, it does it once; the second time it needs the value it gets it from the table. It doesn't compute fib(11) four times; it computes it once, getting it from the table the next three times. Instead of making 1,200 recursive calls to `fib', it makes 15. This makes the function about 150 times faster. You could do the memoization yourself, by rewriting the function, like this: # Compute Fibonacci numbers, memoized version { my @fib; sub fib { my $n = shift; return $fib[$n] if defined $fib[$n]; return $fib[$n] = $n if $n < 2; $fib[$n] = fib($n-1) + fib($n-2); } } Or you could use this module, like this: use Memoize; memoize('fib'); # Rest of the fib function just like the original version. This makes it easy to turn memoizing on and off. Here's an even simpler example: I wrote a simple ray tracer; the program would look in a certain direction, figure out what it was looking at, and then convert the `color' value (typically a string like `red') of that object to a red, green, and blue pixel value, like this: for ($direction = 0; $direction < 300; $direction++) { # Figure out which object is in direction $direction $color = $object->{color}; ($r, $g, $b) = @{&ColorToRGB($color)}; ... } Since there are relatively few objects in a picture, there are only a few colors, which get looked up over and over again. Memoizing "ColorToRGB" sped up the program by several percent.DETAILSThis module exports exactly one function, "memoize". The rest of the functions in this package are None of Your Business. You should say memoize(function) where "function" is the name of the function you want to memoize, or a reference to it. "memoize" returns a reference to the new, memoized version of the function, or "undef" on a non-fatal error. At present, there are no non-fatal errors, but there might be some in the future. If "function" was the name of a function, then "memoize" hides the old version and installs the new memoized version under the old name, so that "&function(...)" actually invokes the memoized version.OPTIONSThere are some optional options you can pass to "memoize" to change the way it behaves a little. To supply options, invoke "memoize" like this: memoize(function, NORMALIZER => function, INSTALL => newname, SCALAR_CACHE => option, LIST_CACHE => option ); Each of these options is optional; you can include some, all, or none of them. INSTALL If you supply a function name with "INSTALL", memoize will install the new, memoized version of the function under the name you give. For example, memoize('fib', INSTALL => 'fastfib') installs the memoized version of "fib" as "fastfib"; without the "INSTALL" option it would have replaced the old "fib" with the memoized version. To prevent "memoize" from installing the memoized version anywhere, use "INSTALL => undef". NORMALIZER Suppose your function looks like this: # Typical call: f('aha!', A => 11, B => 12); sub f { my $a = shift; my %hash = @_; $hash{B} ||= 2; # B defaults to 2 $hash{C} ||= 7; # C defaults to 7 # Do something with $a, %hash } Now, the following calls to your function are all completely equivalent: f(OUCH); f(OUCH, B => 2); f(OUCH, C => 7); f(OUCH, B => 2, C => 7); f(OUCH, C => 7, B => 2); (etc.) However, unless you tell "Memoize" that these calls are equivalent, it will not know that, and it will compute the values for these invocations of your function separately, and store them separately. To prevent this, supply a "NORMALIZER" function that turns the program arguments into a string in a way that equivalent arguments turn into the same string. A "NORMALIZER" function for "f" above might look like this: sub normalize_f { my $a = shift; my %hash = @_; $hash{B} ||= 2; $hash{C} ||= 7; join(',', $a, map ($_ => $hash{$_}) sort keys %hash); } Each of the argument lists above comes out of the "normalize_f" function looking exactly the same, like this: OUCH,B,2,C,7 You would tell "Memoize" to use this normalizer this way: memoize('f', NORMALIZER => 'normalize_f'); "memoize" knows that if the normalized version of the arguments is the same for two argument lists, then it can safely look up the value that it computed for one argument list and return it as the result of calling the function with the other argument list, even if the argument lists look different. The default normalizer just concatenates the arguments with character 28 in between. (In ASCII, this is called FS or control-.) This always works correctly for functions with only one string argument, and also when the arguments never contain character 28. However, it can confuse certain argument lists: normalizer("a 34", "b") normalizer("a", "