繁体   English   中英

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

[英]Performance difference between scipy and numpy norm

我一直认为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

查看源代码显示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大致说明了评估规范所花费的时间差。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM