python使用np.linalg.norm和dist.euclidean计算欧式距离的速度比较

测试代码:

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

points = [[1, 3], [8, 7]]
points = [np.array(val) for val in points]

tic = time.clock()
for i in range(50000):
    temp1 = (np.linalg.norm(points[0] - points[1]))
toc = time.clock()
print("np.linalg.norm: %.3fs" % (toc - tic))

tic = time.clock()
for i in range(50000):
    temp2 = (dist.euclidean(points[0], points[1]))

toc = time.clock()
print("dist.euclidean: %.3fs " % (toc - tic))

测试结果:

np.linalg.norm: 0.343s
dist.euclidean: 0.719s 

结论:np.linalg.norm() 比dist.enclidean()快一倍左右,推荐使用np.linalg.norm() 

你可能感兴趣的:(Python)