Skip to content

Instantly share code, notes, and snippets.

@NorbertFenk
Last active May 3, 2020 23:36
Show Gist options
  • Save NorbertFenk/7bed6760198800207e84f141c41d93c7 to your computer and use it in GitHub Desktop.
Save NorbertFenk/7bed6760198800207e84f141c41d93c7 to your computer and use it in GitHub Desktop.

Comparison betewen the "map as a set" and a string slice accessed by two functions. "Sort" based and "for" based search included too.

Use this command to run the benchmark tests

go test -bench=.
package main
import (
"sort"
)
func main() {}
func containsWithSort(s []string, searchterm string) bool {
i := sort.SearchStrings(s, searchterm)
return i < len(s) && s[i] == searchterm
}
func containsWithFor(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
package main
import (
"testing"
)
var stringSlice = []string{"hello", "world", "good", "bad", "red", "back"}
var mapWithEmptyStruct = map[string]struct{}{
"hello": {},
"world": {},
"good": {},
"bad": {},
"red": {},
"back": {},
}
const (
notElement = "notInTheListOrMap"
element = "good"
)
func BenchmarkContainsWithSortNotFound(b *testing.B) {
for i := 0; i < b.N; i++ {
containsWithSort(stringSlice, notElement)
}
}
func BenchmarkContainsWithSortFound(b *testing.B) {
for i := 0; i < b.N; i++ {
containsWithSort(stringSlice, element)
}
}
func BenchmarkContainsWithForNotFound(b *testing.B) {
for i := 0; i < b.N; i++ {
containsWithFor(stringSlice, notElement)
}
}
func BenchmarkContainsWithForFound(b *testing.B) {
for i := 0; i < b.N; i++ {
containsWithFor(stringSlice, element)
}
}
func BenchmarkContainsMapNotFound(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = mapWithEmptyStruct[notElement]
}
}
func BenchmarkContainsMapFound(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = mapWithEmptyStruct[element]
}
}
@hexnaught
Copy link

For anyone viewing these and interested at a glance, these were my benchmark results. Obviously, these will vary with hardware and current load so run them yourself for a better idea.

CPU: AMD Ryzen 7 3700X

Run 1

Running tool: C:\Go\bin\go.exe test -benchmem -run=^$ gist.github.com/NorbertFenk/7bed6760198800207e84f141c41d93c7 -bench ^(BenchmarkContainsWithSortNotFound|BenchmarkContainsWithSortFound|BenchmarkContainsWithForNotFound|BenchmarkContainsWithForFound|BenchmarkContainsMapNotFound|BenchmarkContainsMapFound)$

goos: windows
goarch: amd64
pkg: gist.github.com/NorbertFenk/7bed6760198800207e84f141c41d93c7
BenchmarkContainsWithSortNotFound-16    	59999400	        19.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsWithSortFound-16       	63158226	        19.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsWithForNotFound-16     	275229483	         4.28 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsWithForFound-16        	274600044	         4.36 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsMapNotFound-16         	158269209	         7.58 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsMapFound-16            	227704286	         5.30 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	gist.github.com/NorbertFenk/7bed6760198800207e84f141c41d93c7	9.541s
Success: Benchmarks passed.

Run 2

goos: windows
goarch: amd64
pkg: gist.github.com/NorbertFenk/7bed6760198800207e84f141c41d93c7
BenchmarkContainsWithSortNotFound-16    	63147924	        19.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsWithSortFound-16       	60002400	        19.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsWithForNotFound-16     	279070026	         4.27 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsWithForFound-16        	275229609	         4.36 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsMapNotFound-16         	158520411	         7.59 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsMapFound-16            	226684977	         5.27 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	gist.github.com/NorbertFenk/7bed6760198800207e84f141c41d93c7	9.556s
Success: Benchmarks passed.

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