-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
45 lines (42 loc) · 1.85 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pairwise_distance_euclidean
import numpy as np
import time
from scipy.spatial.distance import cdist, pdist, squareform
from fastdist import fastdist
if __name__ == "__main__":
# test matrix to matrix distance
matrix_a = np.random.randn(200, 1024)
matrix_b = np.random.randn(1500, 1024)
start = time.monotonic()
for _ in range(10):
dist_cython = pairwise_distance_euclidean.euclidean_matrix_2_matrix(matrix_a, matrix_b)
end = time.monotonic()
print("Cython running time is {:.4f} seconds.".format((end-start) / 10))
start = time.monotonic()
for _ in range(10):
dist_scipy = cdist(matrix_a, matrix_b)
end = time.monotonic()
print("Scipy running time is {:.4f} seconds.".format((end-start) / 10))
start = time.monotonic()
for _ in range(10):
dist_fast = fastdist.matrix_to_matrix_distance(matrix_a, matrix_b, fastdist.euclidean, "euclidean")
end = time.monotonic()
print("Fastdist running time is {:.4f} seconds.".format((end-start) / 10))
assert np.allclose(dist_cython, dist_scipy)
# test pairwise distance
matrix_square = np.random.randn(512, 1024)
start = time.monotonic()
for _ in range(10):
pairwise_cython = pairwise_distance_euclidean.euclidean_pairwise(matrix_square)
end = time.monotonic()
print("Cython running time is {:.4f} seconds.".format((end-start) / 10))
start = time.monotonic()
for _ in range(10):
pairwise_scipy = squareform(pdist(matrix_square))
end = time.monotonic()
print("Scipy running time is {:.4f} seconds.".format((end-start) / 10))
start = time.monotonic()
for _ in range(10):
pairwise_fastdist = fastdist.matrix_pairwise_distance(matrix_square, fastdist.euclidean, "euclidean", True)
end = time.monotonic()
print("Fastdist running time is {:.4f} seconds.".format((end - start) / 10))