【发布时间】:2014-11-15 01:16:57
【问题描述】:
我不明白为什么 numba 在这里击败了 numpy(超过 3 倍)。我在这里进行基准测试时是否犯了一些基本错误?似乎是 numpy 的完美情况,不是吗?请注意,作为检查,我还运行了一个结合 numba 和 numpy 的变体(未显示),正如预期的那样,它与在没有 numba 的情况下运行 numpy 相同。
(顺便说一句,这是Fastest way to numerically process 2d-array: dataframe vs series vs array vs numba 的后续问题)
import numpy as np
from numba import jit
nobs = 10000
def proc_numpy(x,y,z):
x = x*2 - ( y * 55 ) # these 4 lines represent use cases
y = x + y*2 # where the processing time is mostly
z = x + y + 99 # a function of, say, 50 to 200 lines
z = z * ( z - .88 ) # of fairly simple numerical operations
return z
@jit
def proc_numba(xx,yy,zz):
for j in range(nobs): # as pointed out by Llopis, this for loop
x, y = xx[j], yy[j] # is not needed here. it is here by
# accident because in the original benchmarks
x = x*2 - ( y * 55 ) # I was doing data creation inside the function
y = x + y*2 # instead of passing it in as an array
z = x + y + 99 # in any case, this redundant code seems to
z = z * ( z - .88 ) # have something to do with the code running
# faster. without the redundant code, the
zz[j] = z # numba and numpy functions are exactly the same.
return zz
x = np.random.randn(nobs)
y = np.random.randn(nobs)
z = np.zeros(nobs)
res_numpy = proc_numpy(x,y,z)
z = np.zeros(nobs)
res_numba = proc_numba(x,y,z)
结果:
In [356]: np.all( res_numpy == res_numba )
Out[356]: True
In [357]: %timeit proc_numpy(x,y,z)
10000 loops, best of 3: 105 µs per loop
In [358]: %timeit proc_numba(x,y,z)
10000 loops, best of 3: 28.6 µs per loop
我在 2012 macbook air (13.3) 标准 anaconda 发行版上运行此程序。如果相关,我可以提供有关我的设置的更多详细信息。
【问题讨论】:
-
我不明白为什么在 proc_numba 中执行 for 循环,而在 proc_numpy 中却没有
-
@JohnE 你也应该使用 Numexpr(你必须把它写成一个类似字符串的表达式),但应该更接近 numba perf - 它避免了临时性
-
@Llopis 实际上,这只是我最初编写基准的方式的残余。但问题仍然存在,如何(相当愚蠢地)编写它,就像我用额外的步骤所做的那样,实际上最终会导致超过 3 倍的加速?除非我真的从根本上错过了什么(很可能)。
-
@JohnE 您可以通过执行以下操作来优化 numpy 代码:np.add(x,y, out=z) 以避免临时代码(这样做并不好,但应该提高性能)跨度>
-
@Jeff 好的,我之前没有明确使用过 numexpr,但我会尝试弄清楚并稍后添加。很高兴了解 np.add(),但从实际的角度来看,我不确定为什么我不会在这里使用 numba,如果它可以让我写得更简单。