想知道scipy.spatial.distance.sqeuclidean为什么比numpy.sumy1-y2** 2慢两倍?

问题描述

这是我的代码

import numpy as np
import time
from scipy.spatial import distance

y1=np.array([0,1,0])
y2=np.array([0.,0.1,0.,0.7,0.2,0. ])

start_time = time.time()
for i in range(1000000):
    distance.sqeuclidean(y1,y2)
print("--- %s seconds ---" % (time.time() - start_time))

--- 15.212640523910522秒---

start_time = time.time()
for i in range(1000000):
    np.sum((y1-y2)**2)
print("--- %s seconds ---" % (time.time() - start_time))

--- 8.381187438964844 ---秒

我认为Scipy是经过优化的,因此应该更快。

任何评论将不胜感激。

解决方法

这是一个更全面的比较(贷记@Divakar的var getGlobal = function () { // the only reliable means to get the global object is // `Function('return this')()` // However,this causes CSP violations in Chrome apps. if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } throw new Error('unable to locate global object'); }; 软件包):

{
"BookName":{
    "BookAuthor":"BookAuthor","BookCover":"BookCover","BookLink":"BookLink","BookName":"Book","Model3D":"Modelerer","Verified":false
},"Tes":{
    "BookAuthor":"Wut?","BookCover":"Gif","BookLink":"Cuk","Model3D":"Book","Testing":{"
    BookAuthor":"BookAuthor","Model3D":"Modeler Test","Verified":false
}
}

enter image description here

scipy对于较大的阵列效率更高。对于较小的数组,调用该函数的开销很可能超过其好处。根据{{​​3}},scipy计算出benchit

如果您想要更快的解决方案,请直接使用def m1(y1,y2): return distance.sqeuclidean(y1,y2) def m2(y1,y2): return np.sum((y1-y2)**2) in_ = {n:[np.random.rand(n),np.random.rand(n)] for n in [10,100,1000,10000,20000]} ,而无需额外的行和函数调用:

np.dot(y1-y2,y1-y2)

source