Beefy Boxes and Bandwidth Generously Provided by pair Networks
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:

------------------------- | Argument | Return Value | |----------|-------------- | 1 | 1 | | 2 | 2 | | 3 | 6 | | 4 | 24 | | 5 | 120 | | 6 | 720 | | 7 | 5040 | | 8 | 40320 | | 9 | 272160 | | 10 | 2721600 | ---------- -------------- So that if your program wanted to compute <code>fibonacci(5)
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:
use Memoize; memoize('fibonacci');

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
in thread Why is goto &sub slow? by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found