📄 memoize.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "Memoize 3".TH Memoize 3 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"Memoize \- Make functions faster by trading space for time.SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 4\& # This is the documentation for Memoize 1.01\& use Memoize;\& memoize(\*(Aqslow_function\*(Aq);\& slow_function(arguments); # Is faster than it was before.Ve.PPThis is normally all you need to know. However, many options are available:.PP.Vb 1\& memoize(function, options...);.Ve.PPOptions include:.PP.Vb 2\& NORMALIZER => function\& INSTALL => new_name\&\& SCALAR_CACHE => \*(AqMEMORY\*(Aq\& SCALAR_CACHE => [\*(AqHASH\*(Aq, \e%cache_hash ]\& SCALAR_CACHE => \*(AqFAULT\*(Aq\& SCALAR_CACHE => \*(AqMERGE\*(Aq\&\& LIST_CACHE => \*(AqMEMORY\*(Aq\& LIST_CACHE => [\*(AqHASH\*(Aq, \e%cache_hash ]\& LIST_CACHE => \*(AqFAULT\*(Aq\& LIST_CACHE => \*(AqMERGE\*(Aq.Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"`Memoizing' a function makes it faster by trading space for time. Itdoes this by caching the return values of the function in a table.If you call the function again with the same arguments, \f(CW\*(C`memoize\*(C'\fRjumps in and gives you the value out of the table, instead of lettingthe function compute the value all over again..PPHere is an extreme example. Consider the Fibonacci sequence, definedby the following function:.PP.Vb 6\& # Compute Fibonacci numbers\& sub fib {\& my $n = shift;\& return $n if $n < 2;\& fib($n\-1) + fib($n\-2);\& }.Ve.PPThis function is very slow. Why? To compute fib(14), it first wantsto compute fib(13) and fib(12), and add the results. But to computefib(13), it first has to compute fib(12) and fib(11), and then itcomes back and computes fib(12) all over again even though the answeris 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 itagain each time it wants to compute fib(13). This function does somuch recomputing of old results that it takes a really long time torun\-\-\-fib(14) makes 1,200 extra recursive calls to itself, to computeand recompute things that it already computed..PPThis function is a good candidate for memoization. If you memoize the`fib' function above, it will compute fib(14) exactly once, the firsttime it needs to, and then save the result in a table. Then if youask for fib(14) again, it gives you the result out of the table.While computing fib(14), instead of computing fib(12) twice, it doesit 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 itfrom the table the next three times. Instead of making 1,200recursive calls to `fib', it makes 15. This makes the function about150 times faster..PPYou could do the memoization yourself, by rewriting the function, likethis:.PP.Vb 9\& # 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);\& }\& }.Ve.PPOr you could use this module, like this:.PP.Vb 2\& use Memoize;\& memoize(\*(Aqfib\*(Aq);\&\& # Rest of the fib function just like the original version..Ve.PPThis makes it easy to turn memoizing on and off..PPHere's an even simpler example: I wrote a simple ray tracer; theprogram would look in a certain direction, figure out what it waslooking at, and then convert the `color' value (typically a stringlike `red') of that object to a red, green, and blue pixel value, likethis:.PP.Vb 6\& for ($direction = 0; $direction < 300; $direction++) {\& # Figure out which object is in direction $direction\& $color = $object\->{color};\& ($r, $g, $b) = @{&ColorToRGB($color)};\& ...\& }.Ve.PPSince there are relatively few objects in a picture, there are only afew colors, which get looked up over and over again. Memoizing\&\f(CW\*(C`ColorToRGB\*(C'\fR sped up the program by several percent..SH "DETAILS".IX Header "DETAILS"This module exports exactly one function, \f(CW\*(C`memoize\*(C'\fR. The rest of thefunctions in this package are None of Your Business..PPYou should say.PP.Vb 1\& memoize(function).Ve.PPwhere \f(CW\*(C`function\*(C'\fR is the name of the function you want to memoize, ora reference to it. \f(CW\*(C`memoize\*(C'\fR returns a reference to the new,memoized version of the function, or \f(CW\*(C`undef\*(C'\fR on a non-fatal error.At present, there are no non-fatal errors, but there might be some inthe future..PPIf \f(CW\*(C`function\*(C'\fR was the name of a function, then \f(CW\*(C`memoize\*(C'\fR hides theold version and installs the new memoized version under the old name,so that \f(CW\*(C`&function(...)\*(C'\fR actually invokes the memoized version..SH "OPTIONS".IX Header "OPTIONS"There are some optional options you can pass to \f(CW\*(C`memoize\*(C'\fR to changethe way it behaves a little. To supply options, invoke \f(CW\*(C`memoize\*(C'\fRlike this:.PP.Vb 5\& memoize(function, NORMALIZER => function,\& INSTALL => newname,\& SCALAR_CACHE => option,\& LIST_CACHE => option\& );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -