in reply to Fast hypergeometric calculation in Perl

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.