Reputation:
Why scipy.distance.cdist has a big performance difference between using float32 and float64?
from scipy.spatial import distance
import numpy as np
import time
a_float32 = np.empty((1000000, 512), dtype=np.float32)
b_float32 = np.empty((1, 512), dtype=np.float32)
a_float64 = np.empty((1000000, 512), dtype=np.float64)
b_float64 = np.empty((1, 512), dtype=np.float64)
t1 = time.time()
for i in range(100):
distance.cdist(a_float32, b_float32, "sqeuclidean")
t2 = time.time()
print(t2-t1)
t1 = time.time()
for i in range(100):
distance.cdist(a_float64, b_float64, 'sqeuclidean')
t2 = time.time()
print(t2-t1)
On my computer, this code produces a timing of 130.6998474597931 for float32 and 22.450339794158936 for float64, a whopping 6x difference. What is the reason for such a big difference with float64 being faster than float32?
However the gap seems to become smaller and smaller if instead of b being np.empty((1, 512)), b is np.empty((k, 512)) with k (>1) becoming larger and larger. For example, when k=5, I get 222.25975680351257 for float32 and 110.36117148399353 for float64 (2x). Why the gap becomes smaller when k becomes larger?
Upvotes: 4
Views: 2671
Reputation: 114921
The underlying C code that actually does the computation is implemented using C double
variables, which are 64 bit floating point values. When you pass in arrays of np.float32
, the data must be copied.
For the second part of your question: larger k
means more work, so the overhead of copying the data is a smaller fraction of the overall time.
Upvotes: 6