Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions distance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hnsw

import (
"math"
"reflect"

"github.com/viterin/vek/vek32"
Expand All @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading