Skip to content

Instantly share code, notes, and snippets.

@lloyd
Created July 19, 2017 09:03
Show Gist options
  • Save lloyd/ad6144b263a693154b808b2ed13ff5d1 to your computer and use it in GitHub Desktop.
Save lloyd/ad6144b263a693154b808b2ed13ff5d1 to your computer and use it in GitHub Desktop.
maps vs slices for mapping small sets of ordinals to values
package whatever
import (
"testing"
)
func BenchmarkMapConstruction(b *testing.B) {
space := make([]map[int][]string, b.N)
for n := 0; n < b.N; n++ {
space[n] = map[int][]string{
0: []string{},
1: []string{},
2: []string{},
3: []string{},
}
}
}
func BenchmarkSliceConstruction(b *testing.B) {
space := make([][][]string, b.N)
for n := 0; n < b.N; n++ {
space[n] = make([][]string, 4)
}
}
@lloyd
Copy link
Author

lloyd commented Jul 19, 2017

$  go test -bench=. -run "XXX"
BenchmarkMapConstruction-4     	 3000000	       562 ns/op
BenchmarkSliceConstruction-4   	10000000	       104 ns/op
PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment