Memoize

Memoize - Make functions faster by trading space for time.
Download

Memoize Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Mark-Jason Dominus
  • Publisher web site:
  • http://search.cpan.org/~tyemq/Algorithm-Diff-1.1902/lib/Algorithm/Diff.pm

Memoize Tags


Memoize Description

Memoize - Make functions faster by trading space for time. Memoize - Make functions faster by trading space for time.SYNOPSIS # This is the documentation for Memoize 1.01 use Memoize; memoize('slow_function'); slow_function(arguments); # Is faster than it was beforeThis 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 => SCALAR_CACHE => 'FAULT' SCALAR_CACHE => 'MERGE' LIST_CACHE => 'MEMORY' LIST_CACHE => LIST_CACHE => 'FAULT' LIST_CACHE => 'MERGE'`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 if defined $fib; return $fib = $n if $n < 2; $fib = 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.Requirements:· Perl Requirements: · Perl


Memoize Related Software