Skip to content

Instantly share code, notes, and snippets.

@antonsoroko
Last active March 12, 2025 12:58
Show Gist options
  • Select an option

  • Save antonsoroko/ce512f14192dfd2c7b1c9fd2feae292d to your computer and use it in GitHub Desktop.

Select an option

Save antonsoroko/ce512f14192dfd2c7b1c9fd2feae292d to your computer and use it in GitHub Desktop.
bench: force
$(DOCKER) run --rm -v $(GOPATH):/go -e GOPATH=/go -e GOCACHE=/go-cache -v $(shell pwd):/go/src/$(GO_PKG) -v $(shell go env GOCACHE):/go-cache -u `stat -c "%u:%g" $(shell go env GOCACHE)` --ulimit memlock=67108864 -w /go/src/$(GO_PKG) $(DOCKER_IMAGE):$(TARGET_OS)-$(TARGET_ARCH) go test -benchtime=1000x -bench=. ./library/playcount/
package playcount
import (
"math/rand"
"testing"
"time"
)
var result WatchedState
var WatchedMap = map[uint64]WatchedState{}
var WatchedSlice = []uint64{}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func searchForKeyMap(k uint64) WatchedState {
Mu.RLock()
defer Mu.RUnlock()
return WatchedMap[k]
}
func searchForKeySlice(k uint64) WatchedState {
Mu.RLock()
defer Mu.RUnlock()
for _, v := range WatchedSlice {
if v == k {
return true
}
}
return false
}
func benchmarkSearchForKeySlice(target uint64, b *testing.B) {
var watchedState WatchedState
for i := 0; i < b.N; i++ {
watchedState = searchForKeySlice(target)
}
result = watchedState
}
func insertWatchedKeysSlice(n uint64) {
WatchedSlice = []uint64{}
for i := uint64(0); i < n; i++ {
WatchedSlice = append(WatchedSlice, i)
}
}
func benchmarkSearchForKeyMap(target uint64, b *testing.B) {
var watchedState WatchedState
for i := 0; i < b.N; i++ {
watchedState = searchForKeyMap(target)
}
result = watchedState
}
func insertWatchedKeysMap(n uint64) {
WatchedMap = map[uint64]WatchedState{}
for i := uint64(0); i < n; i++ {
WatchedMap[i] = true
}
}
func BenchmarkSearchForKeyMap1000(b *testing.B) {
insertWatchedKeysMap(1000)
target := uint64(rand.Intn(1000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkSearchForKeyMap(target, b)
}
}
func BenchmarkSearchForKeyMap10000(b *testing.B) {
insertWatchedKeysMap(10000)
target := uint64(rand.Intn(10000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkSearchForKeyMap(target, b)
}
}
func BenchmarkSearchForKeyMap100000(b *testing.B) {
insertWatchedKeysMap(100000)
target := uint64(rand.Intn(100000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkSearchForKeyMap(target, b)
}
}
func BenchmarkSearchForKeySlice1000(b *testing.B) {
insertWatchedKeysSlice(1000)
target := uint64(rand.Intn(1000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkSearchForKeySlice(target, b)
}
}
func BenchmarkSearchForKeySlice10000(b *testing.B) {
insertWatchedKeysSlice(10000)
target := uint64(rand.Intn(10000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkSearchForKeySlice(target, b)
}
}
func BenchmarkSearchForKeySlice100000(b *testing.B) {
insertWatchedKeysSlice(100000)
target := uint64(rand.Intn(100000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkSearchForKeySlice(target, b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment