-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_bench_test.go
79 lines (64 loc) · 1.28 KB
/
set_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
package set
import (
"testing"
)
const size = 100
func testSeqSet(start, count int, b *testing.B) Set[int] {
b.Helper()
items := make([]int, count)
for i := 0; i < count; i++ {
items[i] = start + i
}
return New(items...)
}
func Benchmark_Contains(b *testing.B) {
b.Run("Set", func(b *testing.B) {
set := testSeqSet(0, b.N, b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = set.Contains(i)
}
})
b.Run("Map", func(b *testing.B) {
set := make(map[int]struct{}, b.N)
for i := 0; i < b.N; i++ {
set[i] = struct{}{}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = set[i]
}
})
}
func Benchmark_Equals(b *testing.B) {
b.Run("Set", func(b *testing.B) {
lhs := testSeqSet(0, size, b)
rhs := testSeqSet(0, size, b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lhs.Equals(rhs)
}
})
b.Run("Map", func(b *testing.B) {
lhs := make(map[int]struct{}, size)
rhs := make(map[int]struct{}, size)
for i := 0; i < size; i++ {
lhs[i] = struct{}{}
rhs[i] = struct{}{}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for key := range lhs {
_, _ = rhs[key]
}
}
})
}
func BenchmarkUnion(b *testing.B) {
lhs := testSeqSet(0, size, b)
rhs := testSeqSet(size, size, b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Union(lhs, rhs)
}
}