3 ms·
Speaking of interfaces in Go, can anyone explain this particular result? We've done a benchmark recently at work to test which way we would best represent a set
by Insanity 8y ago
Speaking of interfaces in Go, can anyone explain this particular result?
We've done a benchmark recently at work to test which way we would best represent a set. (Linux, go1.12)
map[string]bool
map[string]struct{}
map[string]interface{} // where we put an empty struct in it.
results:
BenchmarkBoolSet-4 5886572 223 ns/op 39 B/op 0 allocs/op
BenchmarkStructSet-4 6716685 206 ns/op 32 B/op 0 allocs/op
BenchmarkInterfaceSet-4 4299072 313 ns/op 115 B/op 0 allocs/op
(I'm aware this is a bit of a tangent to TFA)
- bouk 8y agoCan’t really say what’s going on here without seeing some code.
- Insanity 8y agoWell the code is the same for all of them (but when using a bool map we assign true instead of empty struct). So we run this code inside a benchmarking function: m := map[int]interface{} // or m := map[int]struct{} for i := 0; i < t.N; i++ { m[i] = struct{}{} } edit: fix code.