Skip to content

Instantly share code, notes, and snippets.

@seriousben
Created October 18, 2019 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seriousben/bbe4d8314171a9599bd4875a35f2ed91 to your computer and use it in GitHub Desktop.
Save seriousben/bbe4d8314171a9599bd4875a35f2ed91 to your computer and use it in GitHub Desktop.
Golang Benchmark Map Allocations
> go test -bench=. -test.benchtime=10s

BenchmarkMapIntZero-12             	389632792	        30.6 ns/op	      48 B/op	       1 allocs/op
BenchmarkMapInt100-12              	16012993	       824 ns/op	    4176 B/op	       3 allocs/op
BenchmarkMapInt1000-12             	 1420974	      8083 ns/op	   57424 B/op	       3 allocs/op
BenchmarkMapInt10000-12            	  149038	     83019 ns/op	  458833 B/op	       3 allocs/op
BenchmarkMapSliceStringZero-12     	365958336	        32.7 ns/op	      48 B/op	       1 allocs/op
BenchmarkMapSliceString100-12      	10588574	      1074 ns/op	    6224 B/op	       3 allocs/op
BenchmarkMapSliceString1000-12     	  809688	     14690 ns/op	   98385 B/op	       3 allocs/op
BenchmarkMapSliceString10000-12    	  107524	    110891 ns/op	  737363 B/op	       3 allocs/op
PASS

package main
import (
"testing"
)
var mapInt map[string]int
func BenchmarkMapIntZero(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapInt = make(map[string]int)
}
}
func BenchmarkMapInt100(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapInt = make(map[string]int, 100)
}
}
func BenchmarkMapInt1000(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapInt = make(map[string]int, 1000)
}
}
func BenchmarkMapInt10000(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapInt = make(map[string]int, 10000)
}
}
var mapSliceString map[string][]string
func BenchmarkMapSliceStringZero(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapSliceString = make(map[string][]string)
}
}
func BenchmarkMapSliceString100(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapSliceString = make(map[string][]string, 100)
}
}
func BenchmarkMapSliceString1000(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapSliceString = make(map[string][]string, 1000)
}
}
func BenchmarkMapSliceString10000(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mapSliceString = make(map[string][]string, 10000)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment