-
Notifications
You must be signed in to change notification settings - Fork 1
/
treap_bench_test.go
131 lines (97 loc) · 2.45 KB
/
treap_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
package treap_test
import (
"testing"
"github.com/lthibault/treap"
)
var (
discard, discardRight *treap.Node
discardNode *treap.Node
)
func BenchmarkInsertSync(b *testing.B) {
// To make this a fair benchmark, let's measure single-inserts to a non-empty,
// balanced tree that is consistent across runs.
root := newPrefilledTreap(handle, 1000)
toInsert := mkTestCases(b.N)
b.ReportAllocs()
b.ResetTimer()
for _, tc := range toInsert {
discard, _ = handle.Insert(root, tc.key, tc.value, tc.weight)
}
}
func BenchmarkSplitSync(b *testing.B) {
var root *treap.Node
for _, tc := range mkTestCases(b.N) {
root, _ = handle.Insert(root, tc.key, tc.value, tc.weight)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
discard, discardRight = handle.Split(root, i)
}
}
func BenchmarkMergeSync(b *testing.B) {
var root *treap.Node
splits := make([]struct{ left, right *treap.Node }, b.N)
for _, tc := range mkTestCases(b.N) {
root, _ = handle.Insert(root, tc.key, tc.value, tc.weight)
}
for i := 0; i < b.N; i++ {
splits[i].left, splits[i].right = handle.Split(root, i)
}
b.ReportAllocs()
b.ResetTimer()
for _, s := range splits {
discard = handle.Merge(s.left, s.right)
}
}
func BenchmarkDeleteSync(b *testing.B) {
root := newPrefilledTreap(handle, b.N)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
discard = handle.Delete(root, i)
}
}
func BenchmarkPopSync(b *testing.B) {
root := newPrefilledTreap(handle, 1000)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, discard = handle.Pop(root)
// don't call free; the popped node is still in use!
}
}
func BenchmarkSetWeightSync(b *testing.B) {
var root *treap.Node
cs := mkTestCases(b.N)
for _, tc := range cs {
root, _ = handle.Insert(root, tc.key, tc.value, tc.weight)
}
b.ReportAllocs()
b.ResetTimer()
for i, tc := range cs {
discard, _ = handle.SetWeight(root, tc.key, i)
}
}
func BenchmarkIterSync(b *testing.B) {
var root *treap.Node
cs := mkTestCases(10)
for _, tc := range cs {
root, _ = handle.Insert(root, tc.key, tc.value, tc.weight)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for it := handle.Iter(root); it.Node != nil; it.Next() {
discardNode = it.Node
}
}
}
func newPrefilledTreap(handle treap.Handle, n int) *treap.Node {
var root *treap.Node
cs := mkTestCases(n)
for _, tc := range cs {
root, _ = handle.Insert(root, tc.key, tc.value, tc.weight)
}
return root
}