-
Notifications
You must be signed in to change notification settings - Fork 19
/
skipset_bench_test.go
158 lines (142 loc) · 3.21 KB
/
skipset_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
package skipset
import (
"math"
"sync"
"testing"
"github.com/zhangyunhao116/fastrand"
)
const (
initsize = 1 << 10 // for `contains` `1Remove9Add90Contains` `1Range9Remove90Add900Contains`
randN = math.MaxUint32
)
func BenchmarkInt64(b *testing.B) {
all := []benchTask[int64]{{
name: "skipset", New: func() anyskipset[int64] {
return New[int64]()
}}}
all = append(all, benchTask[int64]{
name: "skipset(func)", New: func() anyskipset[int64] {
return NewFunc(func(a, b int64) bool {
return a < b
})
}})
all = append(all, benchTask[int64]{
name: "sync.Map", New: func() anyskipset[int64] {
return new(anySyncMap[int64])
}})
rng := fastrand.Int63
benchAdd(b, rng, all)
bench30Add70Contains(b, rng, all)
bench1Remove9Add90Contains(b, rng, all)
bench1Range9Remove90Add900Contains(b, rng, all)
}
func benchAdd[T any](b *testing.B, rng func() T, benchTasks []benchTask[T]) {
for _, v := range benchTasks {
b.Run("Add/"+v.name, func(b *testing.B) {
s := v.New()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
s.Add(rng())
}
})
})
}
}
func bench30Add70Contains[T any](b *testing.B, rng func() T, benchTasks []benchTask[T]) {
for _, v := range benchTasks {
b.Run("30Add70Contains/"+v.name, func(b *testing.B) {
s := v.New()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u := fastrand.Uint32n(10)
if u < 3 {
s.Add(rng())
} else {
s.Contains(rng())
}
}
})
})
}
}
func bench1Remove9Add90Contains[T any](b *testing.B, rng func() T, benchTasks []benchTask[T]) {
for _, v := range benchTasks {
b.Run("1Remove9Add90Contains/"+v.name, func(b *testing.B) {
s := v.New()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u := fastrand.Uint32n(100)
if u < 9 {
s.Add(rng())
} else if u == 10 {
s.Remove(rng())
} else {
s.Contains(rng())
}
}
})
})
}
}
func bench1Range9Remove90Add900Contains[T any](b *testing.B, rng func() T, benchTasks []benchTask[T]) {
for _, v := range benchTasks {
b.Run("1Range9Remove90Add900Contains/"+v.name, func(b *testing.B) {
s := v.New()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u := fastrand.Uint32n(1000)
if u == 0 {
s.Range(func(score T) bool {
return true
})
} else if u > 10 && u < 20 {
s.Remove(rng())
} else if u >= 100 && u < 190 {
s.Add(rng())
} else {
s.Contains(rng())
}
}
})
})
}
}
type benchTask[T any] struct {
name string
New func() anyskipset[T]
}
type anySyncMap[T any] struct {
data sync.Map
}
func (m *anySyncMap[T]) Add(x T) bool {
m.data.Store(x, struct{}{})
return true
}
func (m *anySyncMap[T]) Contains(x T) bool {
_, ok := m.data.Load(x)
return ok
}
func (m *anySyncMap[T]) Remove(x T) bool {
m.data.Delete(x)
return true
}
func (m *anySyncMap[T]) Range(f func(value T) bool) {
m.data.Range(func(key, _ any) bool {
return !f(key.(T))
})
}
func (m *anySyncMap[T]) RangeFrom(start T, f func(value T) bool) {
panic("TODO")
}
func (m *anySyncMap[T]) Len() int {
var i int
m.data.Range(func(_, _ any) bool {
i++
return true
})
return i
}