Skip to content

Instantly share code, notes, and snippets.

@codemartial
Created May 28, 2015 08:59
Show Gist options
  • Save codemartial/751a66551de4145e02b2 to your computer and use it in GitHub Desktop.
Save codemartial/751a66551de4145e02b2 to your computer and use it in GitHub Desktop.
Benchmarking sync.RWMutex vs. sync/atomic Load/Store functions in Go 1.4
package mutexvsatomic_test
import (
"sync"
"sync/atomic"
"testing"
)
var a int32
const (
READS = 000
WRITES = 100
)
var readmu = func(mu *sync.RWMutex) {
for x := 0; x < READS; x++ {
mu.RLock()
_ = a
mu.RUnlock()
}
}
var writemu = func(mu *sync.RWMutex) {
for x := 0; x < WRITES; x++ {
mu.Lock()
a = int32(x)
mu.Unlock()
}
}
var read = func() {
for x := 0; x < READS; x++ {
_ = atomic.LoadInt32(&a)
}
}
var write = func() {
for x := 0; x < WRITES; x++ {
atomic.StoreInt32(&a, int32(x))
}
}
var readuns = func() {
for x := 0; x < READS; x++ {
_ = a
}
}
var writeuns = func() {
for x := 0; x < WRITES; x++ {
a = int32(x)
}
}
func BenchmarkMutex(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg := sync.WaitGroup{}
mu := sync.RWMutex{}
go func() {
wg.Add(1)
writemu(&mu)
wg.Done()
}()
go func() {
wg.Add(1)
readmu(&mu)
wg.Done()
}()
wg.Wait()
}
})
}
func BenchmarkAtomic(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
write()
wg.Done()
}()
go func() {
wg.Add(1)
read()
wg.Done()
}()
wg.Wait()
}
})
}
func BenchmarkUnsafe(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
writeuns()
wg.Done()
}()
go func() {
wg.Add(1)
readuns()
wg.Done()
}()
wg.Wait()
}
})
}
/*
-= Sample Runs on Mid 2014 MacBook Pro 15" (2.5GHz Intel Core i7, 16GB 1600 MHz DDR3 RAM) =-
[1000:10 Concurrent Read:Write]$ for i in {1..5}; do go test -bench . -cpu 8 2>/dev/null; done
PASS
BenchmarkMutex-8 300000 3760 ns/op
BenchmarkAtomic-8 1000000 1031 ns/op
BenchmarkUnsafe-8 3000000 435 ns/op
ok mutexvsatomic 4.208s
PASS
BenchmarkMutex-8 500000 3505 ns/op
BenchmarkAtomic-8 2000000 1026 ns/op
BenchmarkUnsafe-8 5000000 462 ns/op
ok mutexvsatomic 7.277s
PASS
BenchmarkMutex-8 500000 3716 ns/op
BenchmarkAtomic-8 1000000 1010 ns/op
BenchmarkUnsafe-8 5000000 455 ns/op
ok mutexvsatomic 5.792s
PASS
BenchmarkMutex-8 500000 4057 ns/op
BenchmarkAtomic-8 1000000 1080 ns/op
BenchmarkUnsafe-8 5000000 386 ns/op
ok mutexvsatomic 5.696s
PASS
BenchmarkMutex-8 500000 3647 ns/op
BenchmarkAtomic-8 2000000 1026 ns/op
BenchmarkUnsafe-8 3000000 446 ns/op
ok mutexvsatomic 6.581s
[1000:100 Concurrent Read:Write]$ for i in {1..5}; do go test -bench . -cpu 8 2>/dev/null; done
PASS
BenchmarkMutex-8 300000 4520 ns/op
BenchmarkAtomic-8 1000000 4067 ns/op
BenchmarkUnsafe-8 5000000 424 ns/op
ok mutexvsatomic 8.197s
PASS
BenchmarkMutex-8 300000 4463 ns/op
BenchmarkAtomic-8 300000 4111 ns/op
BenchmarkUnsafe-8 3000000 440 ns/op
ok mutexvsatomic 4.682s
PASS
BenchmarkMutex-8 300000 4535 ns/op
BenchmarkAtomic-8 500000 3544 ns/op
BenchmarkUnsafe-8 5000000 428 ns/op
ok mutexvsatomic 5.925s
PASS
BenchmarkMutex-8 300000 4623 ns/op
BenchmarkAtomic-8 1000000 3995 ns/op
BenchmarkUnsafe-8 5000000 446 ns/op
ok mutexvsatomic 8.292s
PASS
BenchmarkMutex-8 300000 4471 ns/op
BenchmarkAtomic-8 500000 3939 ns/op
BenchmarkUnsafe-8 5000000 433 ns/op
ok mutexvsatomic 6.191s
[1000:0 Concurrent Read:Write]$ for i in {1..5}; do go test -bench . -cpu 8 2>/dev/null; done
PASS
BenchmarkMutex-8 500000 3599 ns/op
BenchmarkAtomic-8 2000000 790 ns/op
BenchmarkUnsafe-8 3000000 447 ns/op
ok mutexvsatomic 5.875s
PASS
BenchmarkMutex-8 500000 3553 ns/op
BenchmarkAtomic-8 2000000 773 ns/op
BenchmarkUnsafe-8 3000000 463 ns/op
ok mutexvsatomic 5.902s
PASS
BenchmarkMutex-8 500000 3660 ns/op
BenchmarkAtomic-8 2000000 828 ns/op
BenchmarkUnsafe-8 3000000 481 ns/op
ok mutexvsatomic 6.514s
PASS
BenchmarkMutex-8 500000 3748 ns/op
BenchmarkAtomic-8 2000000 820 ns/op
BenchmarkUnsafe-8 3000000 473 ns/op
ok mutexvsatomic 6.217s
PASS
BenchmarkMutex-8 500000 3724 ns/op
BenchmarkAtomic-8 2000000 854 ns/op
BenchmarkUnsafe-8 3000000 432 ns/op
ok mutexvsatomic 5.937s
[0:100 Concurrent Read:Write]$ for i in {1..5}; do go test -bench . -cpu 8 2>/dev/null; done
PASS
BenchmarkMutex-8 1000000 1378 ns/op
BenchmarkAtomic-8 1000000 2509 ns/op
BenchmarkUnsafe-8 3000000 441 ns/op
ok mutexvsatomic 6.167s
PASS
BenchmarkMutex-8 1000000 1438 ns/op
BenchmarkAtomic-8 500000 2601 ns/op
BenchmarkUnsafe-8 3000000 385 ns/op
ok mutexvsatomic 4.739s
PASS
BenchmarkMutex-8 1000000 1586 ns/op
BenchmarkAtomic-8 1000000 2164 ns/op
BenchmarkUnsafe-8 3000000 414 ns/op
ok mutexvsatomic 5.557s
PASS
BenchmarkMutex-8 1000000 1535 ns/op
BenchmarkAtomic-8 1000000 2179 ns/op
BenchmarkUnsafe-8 3000000 394 ns/op
ok mutexvsatomic 5.657s
PASS
BenchmarkMutex-8 1000000 1540 ns/op
BenchmarkAtomic-8 500000 2591 ns/op
BenchmarkUnsafe-8 5000000 351 ns/op
ok mutexvsatomic 5.303s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment