如何使用numba使用数组中的值重新定义积分以加快积分速度

时间:2018-12-21 06:03:46

标签: python arrays scipy integration numba

我尝试使用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秒

1 个答案:

答案 0 :(得分:1)

请注意,您正在测量分配变量和定义函数的时间。

此外,当作业太小时,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)