Skip to content

Instantly share code, notes, and snippets.

@antonsoroko
Last active June 22, 2021 11:17
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 antonsoroko/ce512f14192dfd2c7b1c9fd2feae292d to your computer and use it in GitHub Desktop.
Save antonsoroko/ce512f14192dfd2c7b1c9fd2feae292d to your computer and use it in GitHub Desktop.
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