Memoize 1.01
Sponsored Links
Memoize 1.01 Ranking & Summary
File size:
0.046 MB
Platform:
Any Platform
License:
Perl Artistic License
Price:
Downloads:
886
Date added:
2007-05-24
Publisher:
Mark-Jason Dominus
Memoize 1.01 description
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 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
`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 doesnt 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.
Heres 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.
SYNOPSIS
# This is the documentation for Memoize 1.01
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
`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 doesnt 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.
Heres 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.
Memoize 1.01 Screenshot
Memoize 1.01 Keywords
CACHE
Memoize 1.01
LIST
SCALAR
to compute
trading space
make functions
fib
Memoize
function
compute
n
time
Memoize 1.01
Libraries
Programming
Bookmark Memoize 1.01
Memoize 1.01 Copyright
WareSeeker periodically updates pricing and software information of Memoize 1.01 full version from the publisher, so some information may be slightly out-of-date. You should confirm all information before relying on it. Software piracy is theft, Using crack, password, serial numbers, registration codes, key generators is illegal and prevent future development of Memoize 1.01 Edition. Download links are directly from our publisher sites, torrent files or links from rapidshare.com, yousendit.com or megaupload.com are not allowed
Featured Software
Want to place your software product here?
Please contact us for consideration.
Contact WareSeeker.com
Related Information
Related Software
Timestamp::Simple is a Perl module with simple methods for timestamping. Free Download
lupengo project is a classic arcade game. Free Download
KObelisk is a tool that connects to the Asterisk PBX and gives information to the user about incoming calls. Free Download
openPlaG is an online function graph plotter, written in PHP. Free Download
Cache View is an extension which displays Googles Cache, Corals Cache, Wayback Machines Cache and more. Free Download
Libckpt is a portable checkpointing tool for Unix. Free Download
M.E.S.H. is a tool that measures distortion between two discret surfaces. Free Download
AutoScan is a utility for network exploration (Samba and nessus client). Free Download
Latest Software
Popular Software
Favourite Software