Created
June 24, 2021 00:35
-
-
Save ajm188/19bfe5221b9ae04758e28065946312ca to your computer and use it in GitHub Desktop.
boolset vs emptystructset
This file contains 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
goos: darwin | |
goarch: amd64 | |
pkg: ajm188.scratch/setsbench | |
cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz | |
BenchmarkSets/bool_set-8 366444 3804 ns/op 2620 B/op 6 allocs/op | |
BenchmarkSets/emptystruct_set-8 303908 3472 ns/op 2363 B/op 6 allocs/op | |
PASS | |
coverage: 0.0% of statements | |
ok ajm188.scratch/setsbench 2.867s |
This file contains 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 main | |
import "testing" | |
var words = []string{ | |
"hello", | |
"world", | |
"this", | |
"is", | |
"a", | |
"test", | |
"some", | |
"of", | |
"the", | |
"words", | |
"will", | |
"be", | |
"repeated", | |
"during", | |
"this", | |
"test", | |
"but", | |
"not", | |
"all", | |
"of", | |
"the", | |
"words", | |
"to", | |
"cover", | |
"insertion", | |
"cases", | |
"and", | |
"also", | |
"to", | |
"cover", | |
"some", | |
"cases", | |
"where", | |
"we", | |
"update", | |
"the", | |
"value", | |
"of", | |
"a", | |
"key", | |
"already", | |
"in", | |
"the", | |
"set", | |
} | |
var ( | |
boolset map[string]bool | |
structset map[string]struct{} | |
) | |
func BenchmarkSets(b *testing.B) { | |
b.Run("bool set", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
boolset = map[string]bool{} | |
for _, word := range words { | |
boolset[word] = true | |
} | |
} | |
}) | |
b.Run("emptystruct set", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
structset = map[string]struct{}{} | |
for _, word := range words { | |
structset[word] = struct{}{} | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment