scipy和numpy范数之间的性能差异

时间:2016-10-20 11:34:03

标签: python numpy scipy

我一直认为scipy.linalg.norm()numpy.linalg.norm()是等价的(用于不接受轴参数的scipy版本,但现在它确实如此)。然而,以下简单的例子会产生显着不同的表现:背后的原因是什么?

In [1]: from scipy.linalg import norm as normsp
In [2]: from numpy.linalg import norm as normnp 
In [3]: import numpy as np
In [4]: a = np.random.random(size=(1000, 2000))

In [5]: %timeit normsp(a)
The slowest run took 5.69 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 2.85 ms per loop

In [6]: %timeit normnp(a)
The slowest run took 6.39 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 558 µs per loop
scipy版本是0.18.1,numpy是1.11.1

1 个答案:

答案 0 :(得分:4)

查看source code会发现scipy有自己的norm函数,该函数包围numpy.linalg.norm或BLAS函数,该函数速度较慢但更好地处理浮点溢出(请参阅有关此PR)的讨论。

但是,在你给出它的例子中,SciPy看起来并不像是使用BLAS函数,所以我认为它不会影响你看到的时差。但在调用numpy版本的规范之前,scipy会做一些其他的检查。特别是,无限制检查a = np.asarray_chkfinite(a)是导致性能差异的可疑因素:

In [103]: %timeit normsp(a)
100 loops, best of 3: 5.1 ms per loop

In [104]: %timeit normnp(a)
1000 loops, best of 3: 744 µs per loop

In [105]: %timeit np.asarray_chkfinite(a)
100 loops, best of 3: 4.13 ms per loop

所以看起来np.asarray_chkfinite大致说明了评估规范所花费的时间差异。