-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap_test.go
124 lines (98 loc) · 2.31 KB
/
map_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
package benchmarks
import (
"math/rand"
"strconv"
"sync"
"testing"
)
// Speed
// - read file
// - check and add symbol map
// - add to reverse symbol map
// - check and add edge map (*2 for both directions)
// - add to UF
// Symbol is <type>:<cid>:<id>, say about 40 bytes. Id is about 4 bytes
// so we're talking about 44 * 100mm = ~5GB for the forward direction.
// BenchmarkSync. Uncontended lock/unlock
// About 23 ns
func BenchmarkSync(b *testing.B) {
var s sync.Mutex
for i := 0; i < b.N; i++ {
s.Lock()
s.Unlock()
}
}
// BenchmarkAlloc. Allocating a small slice
func BenchmarkAlloc(b *testing.B) {
store := make([][]byte, b.N)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
store[i] = make([]byte, 8)
}
}
// BenchmarkAddToMap benchmarks adding something to an unsized map
// About 460 ns
func BenchmarkAddToMap(b *testing.B) {
toadd := make([]string, b.N)
for i := range toadd {
toadd[i] = strconv.Itoa(int(rand.Int63()))
}
m := make(map[string]int)
b.ReportAllocs()
b.ResetTimer()
for i, s := range toadd {
m[s] = i
}
}
// BenchmarkCheckAddToMap benchmarks adding something to an unsized map, checking
// first whether the item is present (it shouldn't be). Minimally slower
// About 460 ns
func BenchmarkCheckAddToMap(b *testing.B) {
toadd := make([]string, b.N)
for i := range toadd {
toadd[i] = strconv.Itoa(int(rand.Int63()))
}
m := make(map[string]int)
b.ReportAllocs()
b.ResetTimer()
for i, s := range toadd {
if _, ok := m[s]; !ok {
m[s] = i
}
}
}
func BenchmarkCheckAddToBigMap(b *testing.B) {
m := make(map[string]int, 1000000)
toadd := make([]string, b.N)
for i := range toadd {
toadd[i] = strconv.Itoa(int(rand.Int63()))
}
for i := 0; i < 1000000; i++ {
m[strconv.Itoa(int(rand.Int63()))] = i
}
b.ReportAllocs()
b.ResetTimer()
for i, s := range toadd {
if _, ok := m[s]; !ok {
m[s] = i
}
}
}
// BenchmarkSizedCheckAddToMap benchmarks adding something to an unsized map, checking
// first whether the item is present (it shouldn't be).
// About 250 ns
func BenchmarkSizedCheckAddToMap(b *testing.B) {
toadd := make([]string, b.N)
for i := range toadd {
toadd[i] = strconv.Itoa(int(rand.Int63()))
}
m := make(map[string]int, b.N)
b.ReportAllocs()
b.ResetTimer()
for i, s := range toadd {
if _, ok := m[s]; !ok {
m[s] = i
}
}
}