-
Notifications
You must be signed in to change notification settings - Fork 0
/
vebt_eval.go
294 lines (247 loc) · 7.22 KB
/
vebt_eval.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"fmt"
"github.com/achimk1704/go-vebt"
"time"
"math"
"math/rand"
"strings"
"strconv"
"unsafe"
)
type vebEval struct {
m int
size int
insert, delete, isMember, successor, predecessor, min, max int
}
type VEB struct {
u, min, max int //universe size, min-, max value
summary *VEB //pointer to summary
cluster []*VEB // array of pointers to each child cluster
}
func main() {
// Default values
maxM := 16
runs := 50
testkeys := ""
treeFullness := ""
var operation string
randomTestKeys := 0
var c VEB
var V VEB
V.u, V.min, V.max = 1,1,1
V.summary = &c
V.cluster = append(V.cluster, &c)
fmt.Println("Sizeof VEB struct in Bytes:", unsafe.Sizeof(c), unsafe.Sizeof(V.cluster))
fmt.Printf("Maximum tree size m (u = 2^m)?\n")
fmt.Scanf("%d", &maxM)
fmt.Printf("operation? Choices: \n[count, insert, delete, member, successor, predecessor, min, max]\n")
fmt.Scanf("%s", &operation)
fmt.Printf("Number of test keys for each operation? [e.g. 100 or 50%%]\n")
fmt.Scanf("%s", &testkeys)
fmt.Printf("Create keys randomly=1 or sequentially=0?\n")
fmt.Scanf("%d", &randomTestKeys)
fmt.Printf("How many runs per tree size (reduce effect of randomized keys)?\n")
fmt.Scanf("%d", &runs)
fmt.Printf("Fullness ratio of tree? [e.g. 100 or 50%%]\n")
fmt.Scanf("%s", &treeFullness)
fmt.Printf("Avg time [ns] for each operation with random generated testkeys for %v runs\n", runs)
fmt.Printf("m (u=2^m)\t%v\n", operation)
// measure time + space for different universe sizes (u = 2^i)
for i := 1; i <= maxM; i++ {
u := int(math.Pow(2, float64(i)))
keyNo := 100
treeInitKeyNo := 100
if strings.Contains(testkeys, "%") {
//relative to u
ratio, _ := strconv.ParseInt(testkeys[:strings.Index(testkeys, "%")], 0, 0)
keyNo = int(u * int(ratio)/100)
if keyNo <= 0 {
keyNo = 1
}
} else {
ratio, _ := strconv.ParseInt(testkeys, 0, 0)
keyNo = int(ratio)
}
if strings.Contains(treeFullness, "%") {
//relative to u
treeFullnessRatio, _ := strconv.ParseInt(treeFullness[:strings.Index(treeFullness, "%")], 0, 0)
treeInitKeyNo = int(u * int(treeFullnessRatio)/100)
if treeInitKeyNo <= 0 {
treeInitKeyNo = 1
}
} else {
treeFullnessRatio, _ := strconv.ParseInt(treeFullness, 0, 0)
treeInitKeyNo = int(treeFullnessRatio)
}
// Create tree
V := vebt.CreateTree(u)
timeSum := 0
// run more than once, otherwise random keys have to much influence
for run := 0; run < runs; run++ {
keys := []int{}
// Fill tree with random keys to given number before performing operation
if operation != "count" {
V.Clear() // clear keys inserted before
initKeys := createKeys(treeInitKeyNo, u, 1)
for l := 0; l < len(initKeys); l++ {
V.Insert(initKeys[l])
}
// Create keys for testing operation with
keys = createKeys(keyNo, u, randomTestKeys)
}
switch operation {
case "count":
timeSum = V.Count()
case "insert":
timeSum += int(insertTime(V, keys).Nanoseconds())
case "delete":
timeSum += int(deleteTime(V, keys).Nanoseconds())
case "member":
timeSum += int(isMemberTime(*V, keys).Nanoseconds())
case "successor":
timeSum += int(successorTime(*V, keys).Nanoseconds())
case "predecessor":
timeSum += int(predecessorTime(*V, keys).Nanoseconds())
case "min":
timeSum += int(minTime(*V, keys).Nanoseconds())
case "max":
timeSum += int(maxTime(*V, keys).Nanoseconds())
}
}
timeSum /= runs * keyNo
fmt.Printf("%v\t\t%v\n", i, timeSum)
}
fmt.Printf("Measuring time for {operation} for different tree fullness rate\n")
fmt.Printf("operation? Choices: \n[insert, delete, member, successor, predecessor, min, max]\n")
fmt.Scanf("%s", &operation)
fmt.Printf("Maximum tree size m (u = 2^m)?\n")
fmt.Scanf("%d", &maxM)
fmt.Printf("How many runs per tree size (reduce effect of randomized keys)?\n")
fmt.Scanf("%d", &runs)
fmt.Printf("Measuring avg %v time [ns] for different tree fullness ratios for %v testkeys\n", strings.ToUpper(operation), testkeys)
fmt.Printf("m\t#keys\t")
for fillRate := 0; fillRate <= 100; fillRate += 10 {
fmt.Printf("%v%%\t", fillRate)
}
fmt.Printf("\n")
for i := 1; i <= maxM; i++ {
u := int(math.Pow(2, float64(i)))
V := vebt.CreateTree(u)
fmt.Printf("%v\t", i)
keyNo := 100
if strings.Contains(testkeys, "%") {
//relative to u
ratio, _ := strconv.ParseInt(testkeys[:strings.Index(testkeys, "%")], 0, 0)
keyNo = int(u * int(ratio)/100)
if keyNo <= 0 {
keyNo = 1
}
} else {
ratio, _ := strconv.ParseInt(testkeys, 0, 0)
keyNo = int(ratio)
}
fmt.Printf("%v\t", keyNo)
for fillRate := 0; fillRate <= 100; fillRate += 10 {
V.Clear()
// Create number of random keys to fill tree (depending on fillRate)
insertKeys := createKeys(int(u * fillRate / 100), u, 1)
// Fill tree
for i := 0; i < len(insertKeys); i++ {
V.Insert(insertKeys[i])
}
timeSum := 0
// Measure average time it takes to insert 1 random key
for r := 0; r < runs; r++ {
keys := createKeys(keyNo, u, randomTestKeys)
switch operation {
case "insert":
timeSum += int(insertTime(V, keys).Nanoseconds())
case "delete":
timeSum += int(deleteTime(V, keys).Nanoseconds())
case "member":
timeSum += int(isMemberTime(*V, keys).Nanoseconds())
case "successor":
timeSum += int(successorTime(*V, keys).Nanoseconds())
case "predecessor":
timeSum += int(predecessorTime(*V, keys).Nanoseconds())
case "min":
timeSum += int(minTime(*V, keys).Nanoseconds())
case "max":
timeSum += int(maxTime(*V, keys).Nanoseconds())
}
}
timeSum /= runs * keyNo
fmt.Printf("%v\t", timeSum)
}
fmt.Printf("\n")
}
}
func insertTime(V *vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.Insert(keys[i])
}
return time.Since(start)
}
func deleteTime(V *vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.Delete(keys[i])
}
return time.Since(start)
}
func isMemberTime(V vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.IsMember(keys[i])
}
return time.Since(start)
}
func successorTime(V vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.Successor(keys[i])
}
return time.Since(start)
}
func predecessorTime(V vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.Predecessor(keys[i])
}
return time.Since(start)
}
func minTime(V vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.Min()
}
return time.Since(start)
}
func maxTime(V vebt.VEB, keys []int) time.Duration {
start := time.Now()
for i := 0; i < len(keys); i++ {
V.Max()
}
return time.Since(start)
}
func createKeys(count, max int, randomized int) []int {
keys := []int{}
for i := 0; i < max; i++ {
keys = append(keys, i)
}
//shuffle order of keys
rand.Seed(time.Now().UnixNano())
if randomized != 0 {
for i := range keys {
j := rand.Intn(i + 1)
keys[i], keys[j] = keys[j], keys[i]
}
}
for count > len(keys) {
rand.Seed(time.Now().UnixNano())
keys = append(keys, rand.Intn(max))
}
return keys[0:count]
}