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