你好,我用 Python 写了一些代码,使用mpmath
任意精度数学模块:
from __future__ import division
from numpy import arctan, sin, absolute, log10
from mpmath import *
import time
imax = 1000001
x = mpf(0)
y = mpf(0)
z = mpf(0)
t = mpf(0)
u = mpf(0)
i = 1
mp.prec = 128
start_time = time.time()
while i < 1000001:
i += 1
x = mpf(x + 1)
y = mpf(y + x * x)
z = mpf(z + sin(y))
t = mpf(t + absolute(z))
u = mpf(u + log10(t))
print("--- %s seconds ---" % (time.time() - start_time))
print x
print y
print z
print t
print u
打印结果大约需要 87 秒。如何改进此代码?我在 Fortran 的朋友写了类似的程序,他只用了 3.14 秒就打印了结果。