Last active
December 12, 2021 09:47
-
-
Save sswt/7177d362840651a0e0dd to your computer and use it in GitHub Desktop.
Numpy dtypes performance on squared euclidean distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import numpy as np | |
nl = np.random.randint(0, 1000, 9*10**6).reshape((3000,3000)) | |
def sq_euclidean(X): | |
XX = np.sum(X * X, axis=1)[:, np.newaxis] | |
Y = X | |
YY = XX.T | |
distances = np.dot(X, Y.T) | |
distances *= -2 | |
distances += XX | |
distances += YY | |
# remove floating point rounding errors | |
np.maximum(distances, 0, distances) | |
distances.flat[::distances.shape[0] + 1] = 0.0 | |
for dtype in ['int16', 'int32', 'int64', 'float32', 'float64']: | |
X = nl.astype(np.dtype(dtype), copy=True) | |
t0 = time.time() | |
_ = sq_euclidean(X) | |
print(f'{dtype}: elapsed {time.time() - t0:.3f}s') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment