```go
func Benchmark_MapKey(b *testing.B) {
var length = 100000
var intMap, strMap = make(map[int]bool), make(map[string]bool)
var intSlice, strSlice = make([]int, length), make([]string, length)
for i := 0; i < length; i++ {
intSlice[i] = i
strSlice[i] = strconv.Itoa(i)
}
b.Run("intSlice", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = intSlice[i%length]
}
})
b.Run("strSlice", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strSlice[i%length]
}
})
b.Run("intSet", func(b *testing.B) {
for i := 0; i < b.N; i++ {
intMap[intSlice[i%length]] = (i%2 == 0)
}
})
b.Run("intGet", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = intMap[intSlice[i%length]]
}
})
b.Run("strSet", func(b *testing.B) {
for i := 0; i < b.N; i++ {
strMap[strSlice[i%length]] = (i%2 == 0)
}
})
b.Run("strGet", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strMap[strSlice[i%length]]
}
})
}
```
```go
goos: windows
goarch: amd64
pkg: xxxxxx
cpu: AMD Ryzen 5 5600G with Radeon Graphics
Benchmark_MapKey/intSlice-12 745706593 1.595 ns/op 0 B/op 0 allocs/op
Benchmark_MapKey/strSlice-12 755215707 1.596 ns/op 0 B/op 0 allocs/op
Benchmark_MapKey/intSet-12 41379309 26.19 ns/op 0 B/op 0 allocs/op
Benchmark_MapKey/intGet-12 51822198 22.27 ns/op 0 B/op 0 allocs/op
Benchmark_MapKey/strSet-12 36364297 28.10 ns/op 0 B/op 0 allocs/op
Benchmark_MapKey/strGet-12 43037130 27.77 ns/op 0 B/op 0 allocs/op
```
有疑问加站长微信联系(非本文作者))
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)