Skip to content

Instantly share code, notes, and snippets.

@ttacon
Created March 5, 2015 17:19
Show Gist options
  • Save ttacon/6cbaef09962b70de4e82 to your computer and use it in GitHub Desktop.
Save ttacon/6cbaef09962b70de4e82 to your computer and use it in GitHub Desktop.
Go: map[string]interface{} vs map[string]string
package mem
import "testing"
// So if you're encoding small types (i.e. for JSON on the fly) there is a difference.
//
// maptypes$ go test -bench=. -benchtime=20s
// testing: warning: no tests to run
// PASS
// Benchmark_MapStringInterface 20000000 1407 ns/op 352 B/op 3 allocs/op
// Benchmark_MapStringString 20000000 1130 ns/op 336 B/op 2 allocs/op
// ok test/mem/maptypes 54.116s
//
func Benchmark_MapStringInterface(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = map[string]interface{}{
"hello": "foo",
}
}
}
func Benchmark_MapStringString(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = map[string]string{
"hello": "foo",
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment