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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.