Skip to content

Instantly share code, notes, and snippets.

@reterVision
Created May 16, 2015 08:38
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 reterVision/ea702044a0c9306c8962 to your computer and use it in GitHub Desktop.
Save reterVision/ea702044a0c9306c8962 to your computer and use it in GitHub Desktop.
Benchmark Testing: RWMutex vs sync/atomic
package main
import (
"sync"
"sync/atomic"
"testing"
)
func Benchmark_Lock(b *testing.B) {
var lock sync.RWMutex
var value int32
for i := 0; i < b.N; i++ {
lock.Lock()
value = 5
lock.Unlock()
}
_ = value
}
func Benchmark_Atomic(b *testing.B) {
var value int32
for i := 0; i < b.N; i++ {
atomic.StoreInt32(&value, 5)
}
_ = value
}
func Benchmark_Lock_Goroutine(b *testing.B) {
var lock sync.RWMutex
var value int32
value = 5
wg := new(sync.WaitGroup)
for i := 0; i < b.N; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
lock.RLock()
defer lock.RUnlock()
_ = value
}(wg)
}
wg.Wait()
}
func Benchmark_Atomic_Goroutine(b *testing.B) {
var value int32
atomic.StoreInt32(&value, 5)
wg := new(sync.WaitGroup)
for i := 0; i < b.N; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
atomic.LoadInt32(&value)
}(wg)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment