Skip to content

Instantly share code, notes, and snippets.

@ggirtsou
Created July 16, 2018 20:39
Show Gist options
  • Save ggirtsou/e94a8275a19ca107ccd2726b16ff977a to your computer and use it in GitHub Desktop.
Save ggirtsou/e94a8275a19ca107ccd2726b16ff977a to your computer and use it in GitHub Desktop.
Some benchmarking just for fine
package main
import "testing"
/**
go test -v -bench=.
goos: linux
goarch: amd64
pkg: github.com/ggirtsou/benchmark
BenchmarkSliceLookup-8 200000 6651 ns/op
BenchmarkMapLookup-8 200000000 7.08 ns/op
PASS
ok github.com/ggirtsou/benchmark 4.774s
*/
// omg please don't do this in production
func BenchmarkSliceLookup(b *testing.B) {
var data []int
for i := 0; i < 1000000; i++ {
data = append(data, i)
}
for n := 0; n < b.N; n++ {
for _, v := range data {
if v == 12345 {
break
}
}
}
}
func BenchmarkMapLookup(b *testing.B) {
var s struct{}
data := make(map[int]struct{})
for i := 0; i < 1000000; i++ {
data[i] = s
}
for n := 0; n < b.N; n++ {
_ = data[12345]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment