创建一个10w * 512 的浮点数矩阵,和一个10 * 512 的int8矩阵。
import numpy as np
m = np.random.rand(100000, 512)
np.save("data/feature/512.npy", m)
n = np.ones((3, 3),dtype = "int8")
np.save("data/feature/hash512.npy", n)
首先测试一下汉明距离计算速度
矩阵大小:100000 * 512
feature:1 * 512
import time
start = time.time()
for i in range(100):
d = distance.cdist(n,a,metric = 'hamming')
end = time.time()
print("cost time:{}".format(end - start))
cost time:32.6211576461792
如果是一次的话大约花费0.2s
我佛了,汉明距离计算时间比欧氏距离慢。。。。。。。。。。。。。。。。尬住了
import numpy as np
from scipy.spatial import distance
m = np.random.rand(100000, 512)
#np.save("data/feature/hash512.npy", n)
n = np.random.rand(1, 512)
import time
start = time.time()
for i in range(100):
d = distance.cdist(m, n,metric = "euclidean")
print(d)
end = time.time()
print("cost time:{}".format(start - end))
cost time:5.971029758453369
在这里插入代码片
[2 5 1 0 3 4]