|
| 1 | +package kdtree |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// RadiusMax is the maximum radius for InRange benchmarks. |
| 9 | +const radiusMax = 0.1 |
| 10 | + |
| 11 | +// BenchmarkInsert benchmarks insertions into an initially empty tree. |
| 12 | +func BenchmarkInsert(b *testing.B) { |
| 13 | + b.StopTimer() |
| 14 | + pts := make([]Point, b.N) |
| 15 | + for i := range pts { |
| 16 | + for j := range pts[i] { |
| 17 | + pts[i][j] = rand.Float64() |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + b.StartTimer() |
| 22 | + var t *T |
| 23 | + for i := range pts { |
| 24 | + t = t.Insert(&T{Point: pts[i]}) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// BenchmarkInsert1000 benchmarks 1000 insertions into an empty tree. |
| 29 | +func BenchmarkInsert1000(b *testing.B) { |
| 30 | + insertSz(1000, b) |
| 31 | +} |
| 32 | + |
| 33 | +// BenchmarkInsert500 benchmarks 500 insertions into an empty tree. |
| 34 | +func BenchmarkInsert500(b *testing.B) { |
| 35 | + insertSz(500, b) |
| 36 | +} |
| 37 | + |
| 38 | +// InsertSz benchmarks inserting sz nodes into an empty tree. |
| 39 | +func insertSz(sz int, b *testing.B) { |
| 40 | + b.StopTimer() |
| 41 | + pts := make([]Point, sz) |
| 42 | + for i := range pts { |
| 43 | + for j := range pts[i] { |
| 44 | + pts[i][j] = rand.Float64() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + b.StartTimer() |
| 49 | + for i := 0; i < b.N; i++ { |
| 50 | + var t *T |
| 51 | + for i := range pts { |
| 52 | + t = t.Insert(&T{Point: pts[i]}) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +// BenchmarkMake1000 benchmarks Make with 1000 nodes. |
| 59 | +func BenchmarkMake1000(b *testing.B) { |
| 60 | + makeSz(1000, b) |
| 61 | +} |
| 62 | + |
| 63 | +// BenchmarkMake500 benchmarks Make with 500 nodes. |
| 64 | +func BenchmarkMake500(b *testing.B) { |
| 65 | + makeSz(500, b) |
| 66 | +} |
| 67 | + |
| 68 | +// MakeSz benchmarks Make with a given number of nodes. |
| 69 | +// The time includes allocating the nodes. |
| 70 | +func makeSz(sz int, b *testing.B) { |
| 71 | + b.StopTimer() |
| 72 | + pts := make([]Point, sz) |
| 73 | + for i := range pts { |
| 74 | + for j := range pts[i] { |
| 75 | + pts[i][j] = rand.Float64() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + b.StartTimer() |
| 80 | + nodes := make([]T, sz) |
| 81 | + nodeps := make([]*T, sz) |
| 82 | + for i := range nodes { |
| 83 | + nodes[i].Point = pts[i] |
| 84 | + nodeps[i] = &nodes[i] |
| 85 | + } |
| 86 | + |
| 87 | + for i := 0; i < b.N; i++ { |
| 88 | + New(nodeps) |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +func BenchmarkMakeInRange1000(b *testing.B) { |
| 94 | + newInRangeSz(1000, b) |
| 95 | +} |
| 96 | + |
| 97 | +func BenchmarkMakeInRange500(b *testing.B) { |
| 98 | + newInRangeSz(500, b) |
| 99 | +} |
| 100 | + |
| 101 | +// newInRangeSz benchmarks InRange function on a tree |
| 102 | +// created with New with the given number of nodes. |
| 103 | +func newInRangeSz(sz int, b *testing.B) { |
| 104 | + b.StopTimer() |
| 105 | + nodes := make([]T, sz) |
| 106 | + nodeps := make([]*T, sz) |
| 107 | + for i := range nodes { |
| 108 | + for j := range nodes[i].Point { |
| 109 | + nodes[i].Point[j] = rand.Float64() |
| 110 | + } |
| 111 | + nodeps[i] = &nodes[i] |
| 112 | + } |
| 113 | + tree := New(nodeps) |
| 114 | + |
| 115 | + points := make([]Point, b.N) |
| 116 | + for i := range points { |
| 117 | + for j := range points[i] { |
| 118 | + points[i][j] = rand.Float64() |
| 119 | + } |
| 120 | + } |
| 121 | + rs := make([]float64, b.N) |
| 122 | + for i := range rs { |
| 123 | + rs[i] = rand.Float64() |
| 124 | + } |
| 125 | + |
| 126 | + pool := make([]*T, 0, sz) |
| 127 | + |
| 128 | + b.StartTimer() |
| 129 | + for i, pt := range points { |
| 130 | + tree.InRange(pt, rs[i]*radiusMax, pool[:0]) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func BenchmarkInsertInRange1000(b *testing.B) { |
| 135 | + insertInRangeSz(1000, b) |
| 136 | +} |
| 137 | + |
| 138 | +func BenchmarkInsertInRange500(b *testing.B) { |
| 139 | + insertInRangeSz(500, b) |
| 140 | +} |
| 141 | + |
| 142 | +// insertInRangeSz benchmarks InRange function on a tree |
| 143 | +// created with repeated calls to Insert with the given number |
| 144 | +// of nodes. |
| 145 | +func insertInRangeSz(sz int, b *testing.B) { |
| 146 | + b.StopTimer() |
| 147 | + var tree *T |
| 148 | + for i := 0; i < sz; i++ { |
| 149 | + n := new(T) |
| 150 | + for j := range n.Point { |
| 151 | + n.Point[j] = rand.Float64() |
| 152 | + } |
| 153 | + tree = tree.Insert(n) |
| 154 | + } |
| 155 | + |
| 156 | + points := make([]Point, b.N) |
| 157 | + for i := range points { |
| 158 | + for j := range points[i] { |
| 159 | + points[i][j] = rand.Float64() |
| 160 | + } |
| 161 | + } |
| 162 | + rs := make([]float64, b.N) |
| 163 | + for i := range rs { |
| 164 | + rs[i] = rand.Float64() |
| 165 | + } |
| 166 | + |
| 167 | + pool := make([]*T, 0, sz) |
| 168 | + |
| 169 | + b.StartTimer() |
| 170 | + for i, pt := range points { |
| 171 | + tree.InRange(pt, rs[i]*radiusMax, pool[:0]) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// BenchmarkInRangeLiner1000 benchmarks computing the in range |
| 176 | +// nodes via a linear scan. |
| 177 | +func BenchmarkInRangeLinear1000(b *testing.B) { |
| 178 | + inRangeLinearSz(1000, b) |
| 179 | +} |
| 180 | + |
| 181 | +// inRangeLinearSz benchmarks computing in range nodes using |
| 182 | +// a linear scan of the given number of nodes. |
| 183 | +func inRangeLinearSz(sz int, b *testing.B) { |
| 184 | + b.StopTimer() |
| 185 | + nodes := make([]T, sz) |
| 186 | + for i := range nodes { |
| 187 | + for j := range nodes[i].Point { |
| 188 | + nodes[i].Point[j] = rand.Float64() |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + points := make([]Point, b.N) |
| 193 | + for i := range points { |
| 194 | + for j := range points[i] { |
| 195 | + points[i][j] = rand.Float64() |
| 196 | + } |
| 197 | + } |
| 198 | + rs := make([]float64, b.N) |
| 199 | + for i := range rs { |
| 200 | + rs[i] = rand.Float64() * radiusMax |
| 201 | + } |
| 202 | + |
| 203 | + local := make([]*T, 0, sz) |
| 204 | + |
| 205 | + b.StartTimer() |
| 206 | + for i, pt := range points { |
| 207 | + local = local[:0] |
| 208 | + rr := rs[i] * rs[i] |
| 209 | + for i := range nodes { |
| 210 | + if nodes[i].Point.sqDist(&pt) < rr { |
| 211 | + local = append(local, &nodes[i]) |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments