Pathologically Eclectic Rubbish Lister | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
In the spirit of TIMTOWTDI, here's another option for people who are interested: It's possible to trade space for computational time by "Memoizing" a function. This consists of storing the answers given by a function in a table that is indexed by the function's arguments. People always use the Fibonacci example, in that context the table would look like this: after the fibonacci function had been memoized, your program would do the following: 1. Return the answer defined in the table if it is defined. 2. Otherwise calculate the answer, put it in the symbol table and then return the value. </code> MJD has made a module called Memoize, and it's very easy to use. Just do this:
It's not perfect for every problem, for example if the answers for your function are dependent on a non-explicit parameter (the "localtime" function comes to mind), your answers will be a little odd. Also, there may be problems in which the return values are so big that it's more efficient to recompute the answer when you need it than look it up in a large table. The same problem occurs when you never call your function with the same parameters, this will obviously make your program slower. It may not be exactly what you're talking about, but your thread made me think of it. :) In reply to Re: Why is goto &sub slow?
by biosysadmin
|
|