Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Forked from mipearson/bm_test.go
Last active August 19, 2021 12:56
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 hnakamur/51611e0a9b80f88fce2dac06b74464b3 to your computer and use it in GitHub Desktop.
Save hnakamur/51611e0a9b80f88fce2dac06b74464b3 to your computer and use it in GitHub Desktop.
Add an increment of a global variable after each op to ensure the Go compiler isn't being clever about optimisation.
package bm
import (
"testing"
)
var mb = map[string]bool{
"alpha": true,
"beta": true,
"gamma": true,
"delta": true,
"epsilon": true,
"zeta": true,
"eta": true,
"theta": true,
"iota": true,
"kappa": true,
"lambda": true,
"mu": true,
"nu": true,
"xi": true,
"omicron": true,
"pi": true,
"rho": true,
"sigma": true,
"tau": true,
"upsilon": true,
"phi": true,
"chi": true,
"psi": true,
"omega": true,
}
var ms = map[string]struct{}{
"alpha": struct{}{},
"beta": struct{}{},
"gamma": struct{}{},
"delta": struct{}{},
"epsilon": struct{}{},
"zeta": struct{}{},
"eta": struct{}{},
"theta": struct{}{},
"iota": struct{}{},
"kappa": struct{}{},
"lambda": struct{}{},
"mu": struct{}{},
"nu": struct{}{},
"xi": struct{}{},
"omicron": struct{}{},
"pi": struct{}{},
"rho": struct{}{},
"sigma": struct{}{},
"tau": struct{}{},
"upsilon": struct{}{},
"phi": struct{}{},
"chi": struct{}{},
"psi": struct{}{},
"omega": struct{}{},
}
var bb int
func inc(truthy bool) {
if truthy {
bb += 1
}
}
func BenchmarkNoOp(B *testing.B) {
bb = 0
for i := 0; i < B.N; i++ {
inc(true)
inc(true)
inc(true)
inc(true)
inc(true)
}
}
func BenchmarkMapBool(B *testing.B) {
bb = 0
var b bool
for i := 0; i < B.N; i++ {
b = mb["alpha"]
inc(b)
b = mb["gamma"]
inc(b)
b = mb["mu"]
inc(b)
b = mb["pi"]
inc(b)
b = mb["omega"]
inc(b)
}
}
func BenchmarkMapStruct(B *testing.B) {
bb = 0
var b bool
for i := 0; i < B.N; i++ {
_, b = ms["alpha"]
inc(b)
_, b = ms["gamma"]
inc(b)
_, b = ms["mu"]
inc(b)
_, b = ms["pi"]
inc(b)
_, b = ms["omega"]
inc(b)
}
}
@hnakamur
Copy link
Author

$ go test -bench . -count=10 -benchmem
goos: linux
goarch: amd64
pkg: github.com/hnakamur/go-map-benchmark
cpu: AMD Ryzen 7 PRO 5850U with Radeon Graphics
BenchmarkNoOp-16                916623603                1.295 ns/op           0 B/op          0 allocs/op
BenchmarkNoOp-16                947062818                1.296 ns/op           0 B/op          0 allocs/op
BenchmarkNoOp-16                913785105                1.280 ns/op           0 B/op          0 allocs/op
BenchmarkNoOp-16                941507858                1.273 ns/op           0 B/op          0 allocs/op
BenchmarkNoOp-16                911431180                1.276 ns/op           0 B/op          0 allocs/op
BenchmarkNoOp-16                946081177                1.259 ns/op           0 B/op          0 allocs/op
BenchmarkNoOp-16                1000000000               0.9821 ns/op          0 B/op          0 allocs/op
BenchmarkNoOp-16                1000000000               0.9820 ns/op          0 B/op          0 allocs/op
BenchmarkNoOp-16                1000000000               0.9694 ns/op          0 B/op          0 allocs/op
BenchmarkNoOp-16                1000000000               0.9641 ns/op          0 B/op          0 allocs/op
BenchmarkMapBool-16             32737788                36.38 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             32924523                36.64 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             29113693                37.01 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             32196271                36.60 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             33253494                37.05 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             30089841                36.68 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             27620382                37.79 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             27759393                36.82 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             32802170                36.63 ns/op            0 B/op          0 allocs/op
BenchmarkMapBool-16             28773949                37.24 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           27321225                43.19 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           28566975                43.33 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           27927062                43.58 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           28560314                42.62 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           27566887                43.75 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           24911251                42.26 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           26996860                43.26 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           28189378                43.84 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           24785774                43.39 ns/op            0 B/op          0 allocs/op
BenchmarkMapStruct-16           26041501                43.13 ns/op            0 B/op          0 allocs/op
PASS
ok      github.com/hnakamur/go-map-benchmark    36.187s
$ go version
go version go1.17 linux/amd64

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