[Tutor] Caching for speed

Dick Moores rdm at rcblue.com
Mon Nov 26 11:22:26 CET 2007


At 12:39 AM 11/26/2007, Alan Gauld wrote:
>Dick,
>
>While the speed up due to caching is interesting I'm not sure
>what your code is supposed to be doing but it looks to me
>like you are effectively doing this:
>
>from mpmath import pi
># long pause
>print pi
>
>Which will be faster than 0.1 seconds I suspect!
>Or am I missing something?
>
>Alan G.

I should have explained that "print pi" line.
Using <http://py77.python.pastebin.com/m6b2b34b7>, here's the output 
for precision = 100:

=================================================
Enter a positive integer to set precision: 100
The result calculated from the first 8 terms of the series is:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

pi to 100 digits is:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
Time was 0 seconds
===================================================

The "print pi" line (line 42) is there to confirm to the user that 
the algorithm in fact does compute pi to the requested precision 
(i.e., number of digits). Remember, I wrote the script as a demo of 
the Chudnovsky algorithm for pi.

Dick




More information about the Tutor mailing list