-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintset_benchmark_test.go
83 lines (70 loc) · 1.38 KB
/
intset_benchmark_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
package frozen_test
import (
"fmt"
"testing"
"github.com/arr-ai/frozen"
)
func benchmarkNewIntSet(b *testing.B, n int) {
b.Helper()
arr, _ := generateIntArrayAndSet(n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
frozen.NewIntSet(arr...)
}
}
func benchmarkWithIntSet(b *testing.B, n int) {
b.Helper()
_, set := generateIntArrayAndSet(n)
multiplier := 2147483647 % n
withouts := make([]int, 0, b.N)
for i := 0; i < b.N; i++ {
withouts = append(withouts, i*multiplier)
}
for _, i := range withouts {
set = set.Without(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.With(i * multiplier)
}
}
func BenchmarkIntSetN(b *testing.B) {
// Uncomment for occasional use
b.Skip()
sizes := []int{
100,
1_000,
10_000,
80_000,
100_000,
200_000,
300_000,
500_000,
1_000_000,
2_000_000,
}
for _, n := range sizes {
b.Run(fmt.Sprintf("New/%d", n), func(b *testing.B) {
benchmarkNewIntSet(b, n)
})
}
for _, n := range sizes {
b.Run(fmt.Sprintf("With/%d", n), func(b *testing.B) {
benchmarkWithIntSet(b, n)
})
}
}
func BenchmarkIntSet(b *testing.B) {
for _, e := range []struct {
name string
n int
}{{"100", 100}, {"1k", 1_000}, {"100k", 100_000}, {"1M", 1_000_000}} {
e := e
b.Run("New/"+e.name, func(b *testing.B) {
benchmarkNewIntSet(b, e.n)
})
b.Run("With/"+e.name, func(b *testing.B) {
benchmarkWithIntSet(b, e.n)
})
}
}