KristofferC / NearestNeighbors.jl

High performance nearest neighbor data structures (KDTree and BallTree) and algorithms for Julia.
Other
413 stars 65 forks source link

avoid some square rooting to check distances #197

Closed KristofferC closed 2 months ago

KristofferC commented 2 months ago
 using Revise, StableRNGs
 using NearestNeighbors
 data = rand(StableRNG(1), 3, 10^4);
 x = BallTree(data);
 v = rand(StableRNG(1), 3);
 using GFlops
 @count_ops inrange(x, v, 0.01)

Master:

Flop Counter: 1491 flop
┌──────┬─────────┐
│      │ Float64 │
├──────┼─────────┤
│  add │     438 │
│  sub │     468 │
│  mul │     555 │
│ sqrt │      30 │
└──────┴─────────┘

PR:

Flop Counter: 1491 flop
┌─────┬─────────┐
│     │ Float64 │
├─────┼─────────┤
│ add │     438 │
│ sub │     468 │
│ mul │     558 │
└─────┴─────────┘

No more square roots 🎉.

dkarrasch commented 2 months ago

Same with KDTree?

KristofferC commented 2 months ago

KDTree already does this.

KristofferC commented 2 months ago

By the way, it is amazing how little resources KDTree is using compared to the BallTree (in this very specific case):

 x = KDTree(data);
 @count_ops inrange(x, v, 0.01)
Flop Counter: 196 flop
┌─────┬─────────┐
│     │ Float64 │
├─────┼─────────┤
│ add │      43 │
│ sub │      89 │
│ mul │      64 │
└─────┴─────────┘