我无法弄清楚为什么numba在这里击败numpy(超过3x).我是否在这里进行基准测试时遇到了一些根本性的错误?看起来像numpy的完美情况,不是吗?请注意,作为一个检查,我还运行了一个组合numba和numpy(未显示)的变体,正如预期的那样,与没有numba的numpy相同.
(顺便说一下这是一个后续问题:数字处理2d阵列的最快方法:数据帧vs系列vs阵列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发行版上运行了这个版本.如果相关,我可以提供有关我的设置的更多详细信息.
我认为这个问题(在某种程度上)强调了从更高级语言调用预编译函数的局限性.假设在C++中你写了类似的东西:
for (int i = 0; i != N; ++i) a[i] = b[i] + c[i] + 2 * d[i];
编译器在编译时看到所有这些,整个表达式.它可以在这里做很多非常聪明的事情,包括优化临时(和循环展开).
然而,在python中,考虑一下发生了什么:当你使用numpy时,每个''+''在np数组类型上使用运算符重载(它们只是连续内存块的薄包装,即低级意义上的数组),并调用to fortran(或C++)函数,它可以超快速地添加.但它只是添加一个,并吐出一个临时的.
我们可以看到,在某种程度上,虽然numpy非常棒,方便且非常快,但它会减慢速度,因为虽然看起来它正在调用快速编译的语言以进行艰苦的工作,但编译器却看不到整个程序,它只是输入孤立的小位.这对编译器来说是非常不利的,尤其是现代编译器,它们非常智能,并且在编写代码时可以在每个周期退出多个指令.
另一方面,Numba使用了jit.因此,在运行时它可以确定不需要临时值,并优化它们.基本上,Numba有机会将程序编译为一个整体,numpy只能调用本身已经预编译的小原子块.
当你问numpy时:
x = x*2 - ( y * 55 )
内部翻译为:
tmp1 = y * 55 tmp2 = x * 2 tmp3 = tmp2 - tmp1 x = tmp3
每个temp都是必须分配,操作然后解除分配的数组.另一方面,Numba一次处理一件物品,而不必处理这些开销.
Numba通常比Numpy甚至Cython更快(至少在Linux上).
这是一个情节(从Numba vs. Cython中偷走:Take 2):
在此基准测试中,已计算成对距离,因此这可能取决于算法.
请注意,在其他平台上可能会有所不同,请参阅Winpython(来自WinPython Cython教程):