> Here is my attempt to convert the C code, not written with speed in mind
> and I was too lazy too time it. :-)
>
> from itertools import izip
>
> def pi():
> result = list()
> d = 0
> e = 0
> f = [2000] * 2801
> for c in xrange(2800, 0, -14):
> for b, g in izip(xrange(c, 1, -1), xrange((c * 2) - 1, 0, -2)):
> d += f[b] * 10000
> h, f[b] = divmod(d, g)
> d = h * b
> h, i = divmod(d, 10000)
> result.append('%.4d' % (e + h))
> e = i
> return ''.join(result)
>
Your version: .36 seconds
It's ugly, but unrolling the divmod into two separate statements is
faster for small operands. The following takes .28 seconds:
from time import clock
from itertools import izip
def pi():
result = list()
d = 0
e = 0
f = [2000] * 2801
for c in xrange(2800, 0, -14):
for b, g in izip(xrange(c, 1, -1), xrange((c * 2) - 1, 0, -2)):
d += f[b] * 10000
f[b] = d % g
h = d // g
d = h * b
h, i = divmod(d, 10000)
result.append('%.4d' % (e + h))
e = i
return ''.join(result)
start_time = clock()
pi = pi()
print pi
print "Total time elapsed:", round(clock() - start_time, 2), "s"
print len(pi)
casevh
--
http://mail.python.org/mailman/listinfo/python-list