对数组的两个值进行更快的循环操作

Faster loop operating on two values of an array

考虑以下函数:

def dostuff(n, f):
    array = numpy.arange(0, n)
    for i in range(1, n):                    # Line 1                      
        array[i] = f(array[i-1], array[i])   # Line 2
    return numpy.sum(array)

如何重写第 1 行/第 2 行以使 python 3 中的循环更快(不使用 cython)?

我鼓励你在 SO generalized cumulative functions in NumPy/SciPy? 上检查这个问题,因为你想要 generalized cumulative function .

还要检查 scipy 函数的文档 frompyfunc Here

func = np.frompyfunc(f , 2 , 1)

def dostuff(n,f):
    final_array = func.accumulate(np.arange(0,n), dtype=np.object).astype(np.int)
    return np.sum(final_array)

例子


In [86]:
def f(num1 , num2):
    return num1 + num2

In [87]:
func = np.frompyfunc(f , 2 , 1)

In [88]:
def dostuff(n,f):
    final_array = func.accumulate(np.arange(0,n), dtype=np.object).astype(np.int)
    return np.sum(final_array)

In [108]:
dostuff(15,f)
Out[108]:
560

In [109]:
dostuff(10,f)
Out[109]:
165

基准


def dostuff1(n, f):
    array = np.arange(0, n)
    for i in range(1, n):                    # Line 1                      
        array[i] = f(array[i-1], array[i])   # Line 2
    return np.sum(array)

def dostuff2(n,f):
    final_array = func.accumulate(np.arange(0,n), dtype=np.object).astype(np.int)
    return np.sum(final_array)

In [126]:
%timeit dostuff1(100,f)
10000 loops, best of 3: 40.6 µs per loop

In [127]:
%timeit dostuff2(100,f)
The slowest run took 4.98 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 23.8 µs per loop