如何使用 numba 加速积分,使用数组中的值重新定义积分
How to use numba for speeding up integration using values from array to redefine the integral
我尝试使用 python numba 来更快地计算积分。尽管使用 numba 的时间对于单个计算来说快了近 10 倍,但当我循环重新定义积分的过程时,它变得非常慢。我曾尝试使用其他装饰器,如 @vectorize
或 @jit
但没有成功。关于如何操作的任何提示?
import numpy as np
import datetime as dd
from scipy.integrate import quad
from numba import cfunc, types, carray
tempText = 'Time Elapsed: {0:.6f} sec'
arr = np.arange(0.01,1.01,0.01)
out = np.zeros_like(arr)
def tryThis(): # beginner's solution
for i in range(len(arr)):
def integrand(t):
return np.exp(-arr[i]*t)/t**2
def do_integrate(func):
return quad(func,1,np.inf)[0]
out[i] = do_integrate(integrand)
# print (out)
init = dd.datetime.now()
tryThis()
print (tempText.format((dd.datetime.now()-init).total_seconds()))
已用时间:0.047950 秒
def try2VectorizeThat(): # using numpy
def do_integrate(arr):
def integrand(t):
return np.exp(-arr*t)/t**2
return quad(integrand,1,np.inf)[0]
do_integrate = np.vectorize(do_integrate)
out = do_integrate(arr)
# print (out)
init = dd.datetime.now()
try2VectorizeThat()
print (tempText.format((dd.datetime.now()-init).total_seconds()))
已用时间:0.026424 秒
def tryThisFaster(): # attempting to use numba
for i in range(len(arr)):
def get_integrand(*args):
a = args[0]
def integrand(t):
return np.exp(-a*t)/t**2
return integrand
nb_integrand = cfunc("float64(float64)")(get_integrand(arr[i]))
def do_integrate(func):
return quad(func,1,np.inf)[0]
out[i] = do_integrate(nb_integrand.ctypes)
# print (out)
init = dd.datetime.now()
tryThisFaster()
print (tempText.format((dd.datetime.now()-init).total_seconds()))
已用时间:1.905140 秒
请注意,您正在衡量分配变量和定义包含的函数的时间。
此外,当作业太小时,numba
可能会变得(或看起来)变慢,因为它需要时间来编译自身然后应用。
将 integrand
放在循环外并用 @njit
修饰可以提高性能。让我们看一些比较:
from numba import njit
@njit
def integrand(t, i):
return np.exp(-arr[i]*t)/t**2
def tryFaster():
for i in range(len(arr)):
out[i] = quad(integrand, 1, np.inf, args=(i))[0]
len(arr) = 100
所用时间:
arr = np.arange(0.01,1.01,0.01)
%timeit tryThis()
# 29.9 ms ± 4.59 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit tryFaster()
# 4.99 ms ± 11.5 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
len(arr) = 10,000
所用时间:
arr = np.arange(0.01,100.01,0.01)
%timeit tryThis()
# 1.43 s ± 208 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit tryFaster()
# 142 ms ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
我尝试使用 python numba 来更快地计算积分。尽管使用 numba 的时间对于单个计算来说快了近 10 倍,但当我循环重新定义积分的过程时,它变得非常慢。我曾尝试使用其他装饰器,如 @vectorize
或 @jit
但没有成功。关于如何操作的任何提示?
import numpy as np
import datetime as dd
from scipy.integrate import quad
from numba import cfunc, types, carray
tempText = 'Time Elapsed: {0:.6f} sec'
arr = np.arange(0.01,1.01,0.01)
out = np.zeros_like(arr)
def tryThis(): # beginner's solution
for i in range(len(arr)):
def integrand(t):
return np.exp(-arr[i]*t)/t**2
def do_integrate(func):
return quad(func,1,np.inf)[0]
out[i] = do_integrate(integrand)
# print (out)
init = dd.datetime.now()
tryThis()
print (tempText.format((dd.datetime.now()-init).total_seconds()))
已用时间:0.047950 秒
def try2VectorizeThat(): # using numpy
def do_integrate(arr):
def integrand(t):
return np.exp(-arr*t)/t**2
return quad(integrand,1,np.inf)[0]
do_integrate = np.vectorize(do_integrate)
out = do_integrate(arr)
# print (out)
init = dd.datetime.now()
try2VectorizeThat()
print (tempText.format((dd.datetime.now()-init).total_seconds()))
已用时间:0.026424 秒
def tryThisFaster(): # attempting to use numba
for i in range(len(arr)):
def get_integrand(*args):
a = args[0]
def integrand(t):
return np.exp(-a*t)/t**2
return integrand
nb_integrand = cfunc("float64(float64)")(get_integrand(arr[i]))
def do_integrate(func):
return quad(func,1,np.inf)[0]
out[i] = do_integrate(nb_integrand.ctypes)
# print (out)
init = dd.datetime.now()
tryThisFaster()
print (tempText.format((dd.datetime.now()-init).total_seconds()))
已用时间:1.905140 秒
请注意,您正在衡量分配变量和定义包含的函数的时间。
此外,当作业太小时,numba
可能会变得(或看起来)变慢,因为它需要时间来编译自身然后应用。
将 integrand
放在循环外并用 @njit
修饰可以提高性能。让我们看一些比较:
from numba import njit
@njit
def integrand(t, i):
return np.exp(-arr[i]*t)/t**2
def tryFaster():
for i in range(len(arr)):
out[i] = quad(integrand, 1, np.inf, args=(i))[0]
len(arr) = 100
所用时间:
arr = np.arange(0.01,1.01,0.01)
%timeit tryThis()
# 29.9 ms ± 4.59 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit tryFaster()
# 4.99 ms ± 11.5 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
len(arr) = 10,000
所用时间:
arr = np.arange(0.01,100.01,0.01)
%timeit tryThis()
# 1.43 s ± 208 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit tryFaster()
# 142 ms ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)