mrguy123:

Have you tried to use Memoize to cache the results of logfact? If it's repeatedly being called with the same values, you might get a speed boost. You could try to Memoize hypergeom as well--but if there are too many combinations of arguments (i.e., the argument list rarely repeats) it would be a loss.

...roboticus

Update: Just for my own amusement, I added Memoize to the program and found no significant improvements. Apparently the functions are fast enough that there's no big win to be found simply by caching the values. I replaced the single call at the end of the program with a nested loop (to generate enough runtime to be significant). This should call hypergeom 203,401 times:

for my $i (280 .. 320) { for my $j (680 .. 720) { for my $k (95 .. 105) { for my $l (30 .. 40) { my $hg = hypergeom($i, $j, $k, $l); } } } }

The results are:

39.11s Without Memoize 35.25s Memoize logfact only 38.40s Memoize gammaln only 47.04s Memoize hypergeom only

Of course, with the usage pattern given, I may have artificially improved/reduced Memoize's results, so your mileage will vary.


In reply to Re: Fast hypergeometric calculation in Perl by roboticus
in thread Fast hypergeometric calculation in Perl by mrguy123

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.