Last active
April 18, 2016 16:34
-
-
Save dtjm/d232bf2ddb56d9e1349ca627450dba02 to your computer and use it in GitHub Desktop.
Go map key optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package map_test | |
import ( | |
"testing" | |
) | |
func BenchmarkMapStringKey(b *testing.B) { | |
var m map[string]string = make(map[string]string) | |
var k string = "key" | |
for i := 0; i < b.N; i++ { | |
_, _ = m[k] | |
} | |
} | |
func BenchmarkMapBytesKey(b *testing.B) { | |
var m map[string]string = make(map[string]string) | |
var k []byte = []byte("key") | |
for i := 0; i < b.N; i++ { | |
_, _ = m[string(k)] | |
} | |
} | |
func BenchmarkMapBytesConvertKey(b *testing.B) { | |
var m map[string]string = make(map[string]string) | |
var k []byte = []byte("key") | |
for i := 0; i < b.N; i++ { | |
var ks = string(k) | |
_, _ = m[ks] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -bench=. -benchmem map_test.go | |
testing: warning: no tests to run | |
PASS | |
BenchmarkMapStringKey-4 300000000 4.57 ns/op 0 B/op 0 allocs/op | |
BenchmarkMapBytesKey-4 200000000 7.00 ns/op 0 B/op 0 allocs/op | |
BenchmarkMapBytesConvertKey-4 100000000 20.8 ns/op 0 B/op 0 allocs/op | |
ok command-line-arguments 13.046s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment