1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func BenchmarkHashStringSpeed(b
*testing
.B
) {
15 strings
:= make([]string, size
)
16 for i
:= 0; i
< size
; i
++ {
17 strings
[i
] = fmt
.Sprintf("string#%d", i
)
20 m
:= make(map[string]int, size
)
21 for i
:= 0; i
< size
; i
++ {
26 for i
:= 0; i
< b
.N
; i
++ {
27 sum
+= m
[strings
[idx
]]
37 func BenchmarkHashBytesSpeed(b
*testing
.B
) {
38 // a bunch of chunks, each with a different alignment mod 16
39 var chunks
[size
]chunk
40 // initialize each to a different value
41 for i
:= 0; i
< size
; i
++ {
42 chunks
[i
][0] = byte(i
)
45 m
:= make(map[chunk
]int, size
)
46 for i
, c
:= range chunks
{
51 for i
:= 0; i
< b
.N
; i
++ {
52 if m
[chunks
[idx
]] != idx
{
53 b
.Error("bad map entry for chunk")
62 func BenchmarkHashInt32Speed(b
*testing
.B
) {
63 ints
:= make([]int32, size
)
64 for i
:= 0; i
< size
; i
++ {
68 m
:= make(map[int32]int, size
)
69 for i
:= 0; i
< size
; i
++ {
74 for i
:= 0; i
< b
.N
; i
++ {
83 func BenchmarkHashInt64Speed(b
*testing
.B
) {
84 ints
:= make([]int64, size
)
85 for i
:= 0; i
< size
; i
++ {
89 m
:= make(map[int64]int, size
)
90 for i
:= 0; i
< size
; i
++ {
95 for i
:= 0; i
< b
.N
; i
++ {
103 func BenchmarkHashStringArraySpeed(b
*testing
.B
) {
104 stringpairs
:= make([][2]string, size
)
105 for i
:= 0; i
< size
; i
++ {
106 for j
:= 0; j
< 2; j
++ {
107 stringpairs
[i
][j
] = fmt
.Sprintf("string#%d/%d", i
, j
)
111 m
:= make(map[[2]string]int, size
)
112 for i
:= 0; i
< size
; i
++ {
113 m
[stringpairs
[i
]] = 0
117 for i
:= 0; i
< b
.N
; i
++ {
118 sum
+= m
[stringpairs
[idx
]]
126 func BenchmarkMegMap(b
*testing
.B
) {
127 m
:= make(map[string]bool)
128 for suffix
:= 'A'; suffix
<= 'G'; suffix
++ {
129 m
[strings
.Repeat("X", 1<<20-1)+fmt
.Sprint(suffix
)] = true
131 key
:= strings
.Repeat("X", 1<<20-1) + "k"
133 for i
:= 0; i
< b
.N
; i
++ {
138 func BenchmarkMegOneMap(b
*testing
.B
) {
139 m
:= make(map[string]bool)
140 m
[strings
.Repeat("X", 1<<20)] = true
141 key
:= strings
.Repeat("Y", 1<<20)
143 for i
:= 0; i
< b
.N
; i
++ {
148 func BenchmarkMegEqMap(b
*testing
.B
) {
149 m
:= make(map[string]bool)
150 key1
:= strings
.Repeat("X", 1<<20)
151 key2
:= strings
.Repeat("X", 1<<20) // equal but different instance
154 for i
:= 0; i
< b
.N
; i
++ {
159 func BenchmarkMegEmptyMap(b
*testing
.B
) {
160 m
:= make(map[string]bool)
161 key
:= strings
.Repeat("X", 1<<20)
163 for i
:= 0; i
< b
.N
; i
++ {
168 func BenchmarkSmallStrMap(b
*testing
.B
) {
169 m
:= make(map[string]bool)
170 for suffix
:= 'A'; suffix
<= 'G'; suffix
++ {
171 m
[fmt
.Sprint(suffix
)] = true
175 for i
:= 0; i
< b
.N
; i
++ {
180 func BenchmarkMapStringKeysEight_16(b
*testing
.B
) { benchmarkMapStringKeysEight(b
, 16) }
181 func BenchmarkMapStringKeysEight_32(b
*testing
.B
) { benchmarkMapStringKeysEight(b
, 32) }
182 func BenchmarkMapStringKeysEight_64(b
*testing
.B
) { benchmarkMapStringKeysEight(b
, 64) }
183 func BenchmarkMapStringKeysEight_1M(b
*testing
.B
) { benchmarkMapStringKeysEight(b
, 1<<20) }
185 func benchmarkMapStringKeysEight(b
*testing
.B
, keySize
int) {
186 m
:= make(map[string]bool)
187 for i
:= 0; i
< 8; i
++ {
188 m
[strings
.Repeat("K", i
+1)] = true
190 key
:= strings
.Repeat("K", keySize
)
192 for i
:= 0; i
< b
.N
; i
++ {
197 func BenchmarkIntMap(b
*testing
.B
) {
198 m
:= make(map[int]bool)
199 for i
:= 0; i
< 8; i
++ {
203 for i
:= 0; i
< b
.N
; i
++ {
208 // Accessing the same keys in a row.
209 func benchmarkRepeatedLookup(b
*testing
.B
, lookupKeySize
int) {
210 m
:= make(map[string]bool)
211 // At least bigger than a single bucket:
212 for i
:= 0; i
< 64; i
++ {
213 m
[fmt
.Sprintf("some key %d", i
)] = true
215 base
:= strings
.Repeat("x", lookupKeySize
-1)
219 for i
:= 0; i
< b
.N
/4; i
++ {
227 func BenchmarkRepeatedLookupStrMapKey32(b
*testing
.B
) { benchmarkRepeatedLookup(b
, 32) }
228 func BenchmarkRepeatedLookupStrMapKey1M(b
*testing
.B
) { benchmarkRepeatedLookup(b
, 1<<20) }
230 func BenchmarkNewEmptyMap(b
*testing
.B
) {
232 for i
:= 0; i
< b
.N
; i
++ {
233 _
= make(map[int]int)
237 func BenchmarkMapIter(b
*testing
.B
) {
238 m
:= make(map[int]bool)
239 for i
:= 0; i
< 8; i
++ {
243 for i
:= 0; i
< b
.N
; i
++ {
249 func BenchmarkMapIterEmpty(b
*testing
.B
) {
250 m
:= make(map[int]bool)
252 for i
:= 0; i
< b
.N
; i
++ {
258 func BenchmarkSameLengthMap(b
*testing
.B
) {
259 // long strings, same length, differ in first few
260 // and last few bytes.
261 m
:= make(map[string]bool)
262 s1
:= "foo" + strings
.Repeat("-", 100) + "bar"
263 s2
:= "goo" + strings
.Repeat("-", 100) + "ber"
267 for i
:= 0; i
< b
.N
; i
++ {