Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Actually, there is a simple method to speed this up quite significantly. Since the only needed gamma values are at integers (up to a few several thousand), you can just store all of them in a list or a dict.

In fact mpmath provides a decorator "memoize" that effectively does this. So you could just add

gamma = memoize(gamma)

to the program. With this change, the test case myfisher_mp(1286, 9548, 133437, 148905) runs about 2x faster the first time, and 7x faster the second time. But it should be even faster if you precompute the needed gamma values and replace the function calls with list lookups.

Actually, there is a simple method to speed this up quite significantly. Since the only needed gamma values are at integers (up to a few several thousand), you can just store all of them in a list or a dict.

In fact mpmath provides a decorator "memoize" that effectively does this. So you could just add

gamma = memoize(gamma)

to the program. With this change, the test case myfisher_mp(1286, 9548, 133437, 148905) runs about 2x faster the first time, and 7x faster the second time. But it should be even faster if you precompute the needed gamma values and replace the function calls with list lookups.lookups. Note that to precompute a list up to say n = 100000, you could just use a loop instead of repeatedly calling gamma, and this would be very fast.