forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
167 lines (131 loc) · 3.41 KB
/
benchmarks_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
package quadtree
import (
"math"
"math/rand"
"testing"
"github.com/paulmach/orb"
"github.com/paulmach/orb/planar"
)
func BenchmarkAdd(b *testing.B) {
r := rand.New(rand.NewSource(22))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
qt.Add(orb.Point{r.Float64(), r.Float64()})
}
}
func BenchmarkRandomFind1000(b *testing.B) {
r := rand.New(rand.NewSource(42))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
for i := 0; i < 1000; i++ {
qt.Add(orb.Point{r.Float64(), r.Float64()})
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
qt.Find(orb.Point{r.Float64(), r.Float64()})
}
}
func BenchmarkRandomFind1000Naive(b *testing.B) {
r := rand.New(rand.NewSource(42))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
points := []orb.Point{}
for i := 0; i < 1000; i++ {
p := orb.Point{r.Float64(), r.Float64()}
qt.Add(p)
points = append(points, p)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
looking := orb.Point{r.Float64(), r.Float64()}
min := math.MaxFloat64
var best orb.Point
for _, p := range points {
if d := planar.DistanceSquared(looking, p); d < min {
min = d
best = p
}
}
_ = best
}
}
func BenchmarkRandomInBound1000(b *testing.B) {
r := rand.New(rand.NewSource(43))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
for i := 0; i < 1000; i++ {
qt.Add(orb.Point{r.Float64(), r.Float64()})
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := orb.Point{r.Float64(), r.Float64()}
qt.InBound(nil, p.Bound().Pad(0.1))
}
}
func BenchmarkRandomInBound1000Naive(b *testing.B) {
r := rand.New(rand.NewSource(43))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
points := []orb.Point{}
for i := 0; i < 1000; i++ {
p := orb.Point{r.Float64(), r.Float64()}
qt.Add(p)
points = append(points, p)
}
b.ReportAllocs()
b.ResetTimer()
var near []orb.Point
for i := 0; i < b.N; i++ {
p := orb.Point{r.Float64(), r.Float64()}
b := orb.Bound{Min: p, Max: p}
b = b.Pad(0.1)
near = near[:0]
for _, p := range points {
if b.Contains(p) {
near = append(near, p)
}
}
_ = len(near)
}
}
func BenchmarkRandomInBound1000Buf(b *testing.B) {
r := rand.New(rand.NewSource(43))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
for i := 0; i < 1000; i++ {
qt.Add(orb.Point{r.Float64(), r.Float64()})
}
var buf []orb.Pointer
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := orb.Point{r.Float64(), r.Float64()}
buf = qt.InBound(buf, p.Bound().Pad(0.1))
}
}
func BenchmarkRandomKNearest10(b *testing.B) {
r := rand.New(rand.NewSource(43))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
for i := 0; i < 1000; i++ {
qt.Add(orb.Point{r.Float64(), r.Float64()})
}
buf := make([]orb.Pointer, 0, 10)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
qt.KNearest(buf[:0], orb.Point{r.Float64(), r.Float64()}, 10)
}
}
func BenchmarkRandomKNearest100(b *testing.B) {
r := rand.New(rand.NewSource(43))
qt := New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
for i := 0; i < 1000; i++ {
qt.Add(orb.Point{r.Float64(), r.Float64()})
}
buf := make([]orb.Pointer, 0, 100)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
qt.KNearest(buf[:0], orb.Point{r.Float64(), r.Float64()}, 100)
}
}