LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 package runtime_test | 4 package runtime_test |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | 7 "fmt" |
8 "strings" | 8 "strings" |
9 "testing" | 9 "testing" |
10 ) | 10 ) |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 m := make(map[string]bool) | 131 m := make(map[string]bool) |
132 for suffix := 'A'; suffix <= 'G'; suffix++ { | 132 for suffix := 'A'; suffix <= 'G'; suffix++ { |
133 m[fmt.Sprint(suffix)] = true | 133 m[fmt.Sprint(suffix)] = true |
134 } | 134 } |
135 key := "k" | 135 key := "k" |
136 b.ResetTimer() | 136 b.ResetTimer() |
137 for i := 0; i < b.N; i++ { | 137 for i := 0; i < b.N; i++ { |
138 _, _ = m[key] | 138 _, _ = m[key] |
139 } | 139 } |
140 } | 140 } |
| 141 |
141 func BenchmarkIntMap(b *testing.B) { | 142 func BenchmarkIntMap(b *testing.B) { |
142 m := make(map[int]bool) | 143 m := make(map[int]bool) |
143 for i := 0; i < 8; i++ { | 144 for i := 0; i < 8; i++ { |
144 m[i] = true | 145 m[i] = true |
145 } | 146 } |
146 b.ResetTimer() | 147 b.ResetTimer() |
147 for i := 0; i < b.N; i++ { | 148 for i := 0; i < b.N; i++ { |
148 _, _ = m[7] | 149 _, _ = m[7] |
149 } | 150 } |
150 } | 151 } |
| 152 |
| 153 // Accessing the same keys in a row. |
| 154 func benchmarkRepeatedLookup(b *testing.B, lookupKeySize int) { |
| 155 m := make(map[string]bool) |
| 156 // At least bigger than a single bucket: |
| 157 for i := 0; i < 64; i++ { |
| 158 m[fmt.Sprintf("some key %d", i)] = true |
| 159 } |
| 160 base := strings.Repeat("x", lookupKeySize-1) |
| 161 key1 := base + "1" |
| 162 key2 := base + "2" |
| 163 b.ResetTimer() |
| 164 for i := 0; i < b.N/4; i++ { |
| 165 _ = m[key1] |
| 166 _ = m[key1] |
| 167 _ = m[key2] |
| 168 _ = m[key2] |
| 169 } |
| 170 } |
| 171 |
| 172 func BenchmarkRepeatedLookupStrMapKey32(b *testing.B) { benchmarkRepeatedLookup(
b, 32) } |
| 173 func BenchmarkRepeatedLookupStrMapKey1M(b *testing.B) { benchmarkRepeatedLookup(
b, 1<<20) } |
LEFT | RIGHT |