Skip to content

Instantly share code, notes, and snippets.

@tiraboschi
Created February 13, 2024 21:28
Show Gist options
  • Save tiraboschi/f52efd6dfca29bf1b4482643770f5112 to your computer and use it in GitHub Desktop.
Save tiraboschi/f52efd6dfca29bf1b4482643770f5112 to your computer and use it in GitHub Desktop.
test map hint
stirabos@t14s:/tmp/mapt$ cat main.go
package main
func main() {}
var srcLabels = map[string]string{
"app": "kubevirt-hyperconverged",
"app.kubernetes.io/component": "compute",
"app.kubernetes.io/managed-by": "hco-operator",
"app.kubernetes.io/part-of": "hyperconverged-cluster",
"app.kubernetes.io/version": "4.14.3",
"userk0": "userv0",
"userk1": "userv1",
"userk2": "userv2",
"userk3": "userv3",
"userk4": "userv4",
"userk5": "userv5",
"userk6": "userv6",
"userk7": "userv7",
"userk8": "userv8",
"userk9": "userv9",
}
func MapWithHint() {
m := make(map[string]string, len(srcLabels))
for key, val := range srcLabels {
m[key] = val
}
}
func MapWithoutHint() {
m := make(map[string]string)
for key, val := range srcLabels {
m[key] = val
}
}
stirabos@t14s:/tmp/mapt$ cat main_test.go
package main
import "testing"
func Benchmark_Hint(b *testing.B) {
for i := 0; i < b.N; i++ {
MapWithHint()
}
}
func Benchmark_WithoutHint(b *testing.B) {
for i := 0; i < b.N; i++ {
MapWithoutHint()
}
}
stirabos@t14s:/tmp/mapt$ go test -bench=. -benchtime=10s
goos: linux
goarch: amd64
pkg: example.com/m
cpu: Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
Benchmark_Hint-8 14231425 822.9 ns/op
Benchmark_WithoutHint-8 8742591 1460 ns/op
PASS
ok example.com/m 26.720s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment