diff --git a/distance.go b/distance.go index 4ab7cad..86024dc 100644 --- a/distance.go +++ b/distance.go @@ -1,7 +1,6 @@ package hnsw import ( - "math" "reflect" "github.com/viterin/vek/vek32" @@ -17,13 +16,7 @@ func CosineDistance(a, b []float32) float32 { // EuclideanDistance computes the Euclidean distance between two vectors. func EuclideanDistance(a, b []float32) float32 { - // TODO: can we speedup with vek? - var sum float32 = 0 - for i := range a { - diff := a[i] - b[i] - sum += diff * diff - } - return float32(math.Sqrt(float64(sum))) + return vek32.Distance(a, b) } var distanceFuncs = map[string]DistanceFunc{ diff --git a/distance_test.go b/distance_test.go index e411ee5..11c2c94 100644 --- a/distance_test.go +++ b/distance_test.go @@ -30,6 +30,15 @@ func TestCosineSimilarity(t *testing.T) { require.InDelta(t, 0, CosineDistance(a, b), 0.000001) } +func BenchmarkEuclideanDistance(b *testing.B) { + v1 := randFloats(1536) + v2 := randFloats(1536) + b.ResetTimer() + for i := 0; i < b.N; i++ { + EuclideanDistance(v1, v2) + } +} + func BenchmarkCosineSimilarity(b *testing.B) { v1 := randFloats(1536) v2 := randFloats(1536)