-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmap_benchmark_test.go
122 lines (114 loc) · 2.49 KB
/
armap_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
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
package armap
import (
"runtime"
"sort"
"testing"
"time"
)
func BenchmarkMap(b *testing.B) {
b.Run("map", func(tb *testing.B) {
m := make(map[int]int, tb.N)
for i := 0; i < tb.N; i += 1 {
m[i] = i
}
for i := 0; i < tb.N; i += 1 {
_, _ = m[i]
}
for i := 0; i < tb.N; i += 1 {
delete(m, i)
}
})
b.Run("armap", func(tb *testing.B) {
a := NewArena(1*1024*1024, 400)
defer a.Release()
m := NewMap[int, int](a, WithCapacity(tb.N))
for i := 0; i < tb.N; i += 1 {
m.Set(i, i)
}
for i := 0; i < tb.N; i += 1 {
_, _ = m.Get(i)
}
for i := 0; i < tb.N; i += 1 {
_, _ = m.Delete(i)
}
})
}
func BenchmarkSet(b *testing.B) {
b.Run("map", func(tb *testing.B) {
m := make(map[int]struct{}, tb.N)
for i := 0; i < tb.N; i += 1 {
m[i] = struct{}{}
}
for i := 0; i < tb.N; i += 1 {
_, _ = m[i]
}
for i := 0; i < tb.N; i += 1 {
delete(m, i)
}
})
b.Run("armap", func(tb *testing.B) {
a := NewArena(1*1024*1024, 400)
defer a.Release()
m := NewSet[int](a, WithCapacity(tb.N))
for i := 0; i < tb.N; i += 1 {
m.Add(i)
}
for i := 0; i < tb.N; i += 1 {
_ = m.Contains(i)
}
for i := 0; i < tb.N; i += 1 {
_ = m.Delete(i)
}
})
}
func BenchmarkGCSet(b *testing.B) {
b.Run("golangmap", func(tb *testing.B) {
m := make(map[*int]struct{}, 100_000_000)
tb.ResetTimer()
n := 10
elapse := make([]time.Duration, n)
for i := 0; i < n; i += 1 {
start := time.Now()
runtime.GC()
elapse[i] = time.Since(start)
}
runtime.KeepAlive(m)
tb.StopTimer()
total := int64(0)
for _, e := range elapse {
total += int64(e)
}
sort.Slice(elapse, func(i, j int) bool {
return elapse[i] < elapse[j]
})
mean := time.Duration(float64(total) / float64(n))
median := elapse[4]
tb.Logf("min/avg/max/median = %s/%s/%s/%s", elapse[0], mean, elapse[9], median)
})
b.Run("armap", func(tb *testing.B) {
a := NewArena(1*1024*1024, 100)
m := NewSet[*int](a, WithCapacity(100_000_000))
n := 10
elapse := make([]time.Duration, n)
for i := 0; i < n; i += 1 {
start := time.Now()
runtime.GC()
elapse[i] = time.Since(start)
}
m.Clear()
a.Release()
runtime.KeepAlive(m)
runtime.KeepAlive(a)
tb.StopTimer()
total := int64(0)
for _, e := range elapse {
total += int64(e)
}
sort.Slice(elapse, func(i, j int) bool {
return elapse[i] < elapse[j]
})
mean := time.Duration(float64(total) / float64(n))
median := elapse[4]
tb.Logf("min/avg/max/median = %s/%s/%s/%s", elapse[0], mean, elapse[9], median)
})
}