| // Copyright 2013 The Go Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style | |
| // license that can be found in the LICENSE file. | |
| package runtime_test | |
| import ( | |
| "fmt" | |
| "testing" | |
| ) | |
| const size = 10 | |
| func BenchmarkHashStringSpeed(b *testing.B) { | |
| strings := make([]string, size) | |
| for i := 0; i < size; i++ { | |
| strings[i] = fmt.Sprintf("string#%d", i) | |
| } | |
| sum := 0 | |
| m := make(map[string]int, size) | |
| for i := 0; i < size; i++ { | |
| m[strings[i]] = 0 | |
| } | |
| idx := 0 | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| sum += m[strings[idx]] | |
| idx++ | |
| if idx == size { | |
| idx = 0 | |
| } | |
| } | |
| } | |
| func BenchmarkHashInt32Speed(b *testing.B) { | |
| ints := make([]int32, size) | |
| for i := 0; i < size; i++ { | |
| ints[i] = int32(i) | |
| } | |
| sum := 0 | |
| m := make(map[int32]int, size) | |
| for i := 0; i < size; i++ { | |
| m[ints[i]] = 0 | |
| } | |
| idx := 0 | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| sum += m[ints[idx]] | |
| idx++ | |
| if idx == size { | |
| idx = 0 | |
| } | |
| } | |
| } | |
| func BenchmarkHashInt64Speed(b *testing.B) { | |
| ints := make([]int64, size) | |
| for i := 0; i < size; i++ { | |
| ints[i] = int64(i) | |
| } | |
| sum := 0 | |
| m := make(map[int64]int, size) | |
| for i := 0; i < size; i++ { | |
| m[ints[i]] = 0 | |
| } | |
| idx := 0 | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| sum += m[ints[idx]] | |
| idx++ | |
| if idx == size { | |
| idx = 0 | |
| } | |
| } | |
| } | |
| func BenchmarkHashStringArraySpeed(b *testing.B) { | |
| stringpairs := make([][2]string, size) | |
| for i := 0; i < size; i++ { | |
| for j := 0; j < 2; j++ { | |
| stringpairs[i][j] = fmt.Sprintf("string#%d/%d", i, j) | |
| } | |
| } | |
| sum := 0 | |
| m := make(map[[2]string]int, size) | |
| for i := 0; i < size; i++ { | |
| m[stringpairs[i]] = 0 | |
| } | |
| idx := 0 | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| sum += m[stringpairs[idx]] | |
| idx++ | |
| if idx == size { | |
| idx = 0 | |
| } | |
| } | |
| } |