-
Notifications
You must be signed in to change notification settings - Fork 0
/
levtrie_bench_test.go
223 lines (192 loc) · 4.43 KB
/
levtrie_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
216
217
218
219
220
221
222
223
package levtrie
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"testing"
)
var data []string
var alphabet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var words []string
var suggestData = []string{
"acetonylacetone",
"barbaralalia",
"calcic",
"dark",
"using",
"volt",
"wrenchingly",
"xenos",
"yore",
"zymosis",
}
func randString(n int) string {
runes := make([]rune, n)
for i := range runes {
runes[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(runes)
}
func ensureData(n int) {
if n <= len(data) {
return
}
for i := len(data); i < n; i++ {
data = append(data, randString(rand.Intn(20)))
}
}
func ensureWords() {
if len(words) > 0 {
return
}
filename := "/usr/share/dict/words"
file, err := os.Open(filename)
if err != nil {
panic(fmt.Sprintf("%v: %v", filename, err))
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
word := strings.ToLower(scanner.Text())
words = append(words, word)
}
}
func benchmarkSuggest(d int, b *testing.B) {
ensureWords()
r := New()
for _, word := range words {
r.Set(word, word)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Suggest(suggestData[i%len(suggestData)], int8(d), 10)
}
}
func benchmarkSuggestAfterExactPrefix(d int, p int, b *testing.B) {
ensureWords()
r := New()
for _, word := range words {
r.Set(word, word)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.SuggestAfterExactPrefix(suggestData[i%len(suggestData)], p, int8(d), 10)
}
}
func BenchmarkSuggestTopTenDistance1(b *testing.B) {
benchmarkSuggest(1, b)
}
func BenchmarkSuggestTopTenDistance2(b *testing.B) {
benchmarkSuggest(2, b)
}
func BenchmarkSuggestTopTenDistance3(b *testing.B) {
benchmarkSuggest(3, b)
}
func BenchmarkSuggestTopTenDistance4(b *testing.B) {
benchmarkSuggest(4, b)
}
func BenchmarkSuggestTopTenDistance5(b *testing.B) {
benchmarkSuggest(5, b)
}
func BenchmarkSuggestTopTenDistance6(b *testing.B) {
benchmarkSuggest(6, b)
}
func BenchmarkSuggestAfterLength1PrefixTopTenDistance1(b *testing.B) {
benchmarkSuggestAfterExactPrefix(1, 1, b)
}
func BenchmarkSuggestAfterLength1PrefixTopTenDistance2(b *testing.B) {
benchmarkSuggestAfterExactPrefix(2, 1, b)
}
func BenchmarkSuggestAfterLength1PrefixTopTenDistance3(b *testing.B) {
benchmarkSuggestAfterExactPrefix(3, 1, b)
}
func BenchmarkSuggestAfterLength1PrefixTopTenDistance4(b *testing.B) {
benchmarkSuggestAfterExactPrefix(4, 1, b)
}
func BenchmarkSuggestAfterLength1PrefixTopTenDistance5(b *testing.B) {
benchmarkSuggestAfterExactPrefix(5, 1, b)
}
func BenchmarkSuggestAfterLength1PrefixTopTenDistance6(b *testing.B) {
benchmarkSuggestAfterExactPrefix(6, 1, b)
}
func BenchmarkSuggestAfterLength2PrefixTopTenDistance1(b *testing.B) {
benchmarkSuggestAfterExactPrefix(1, 2, b)
}
func BenchmarkSuggestAfterLength2PrefixTopTenDistance2(b *testing.B) {
benchmarkSuggestAfterExactPrefix(2, 2, b)
}
func BenchmarkSuggestAfterLength2PrefixTopTenDistance3(b *testing.B) {
benchmarkSuggestAfterExactPrefix(3, 2, b)
}
func BenchmarkSuggestAfterLength2PrefixTopTenDistance4(b *testing.B) {
benchmarkSuggestAfterExactPrefix(4, 2, b)
}
func BenchmarkSuggestAfterLength2PrefixTopTenDistance5(b *testing.B) {
benchmarkSuggestAfterExactPrefix(5, 2, b)
}
func BenchmarkSuggestAfterLength2PrefixTopTenDistance6(b *testing.B) {
benchmarkSuggestAfterExactPrefix(6, 2, b)
}
func BenchmarkLevtrieSet(b *testing.B) {
ensureData(b.N)
b.ResetTimer()
r := New()
for i := 0; i < b.N; i++ {
r.Set(data[i], data[i])
}
}
func BenchmarkMapSet(b *testing.B) {
ensureData(b.N)
b.ResetTimer()
m := make(map[string]string)
for i := 0; i < b.N; i++ {
m[data[i]] = data[i]
}
}
func BenchmarkLevtrieGet(b *testing.B) {
ensureData(b.N)
r := New()
for i := 0; i < b.N; i++ {
r.Set(data[i], data[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Get(data[i])
}
}
func BenchmarkMapGet(b *testing.B) {
ensureData(b.N)
m := make(map[string]string)
for i := 0; i < b.N; i++ {
m[data[i]] = data[i]
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m[data[i]]
}
}
func BenchmarkLevtrieDelete(b *testing.B) {
ensureData(b.N)
r := New()
for i := 0; i < b.N; i++ {
r.Set(data[i], data[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Delete(data[i])
}
}
func BenchmarkMapDelete(b *testing.B) {
ensureData(b.N)
m := make(map[string]string)
for i := 0; i < b.N; i++ {
m[data[i]] = data[i]
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
delete(m, data[i])
}
}