Skip to content

Instantly share code, notes, and snippets.

@gotterdemarung
Created May 3, 2017 08:00
Show Gist options
  • Save gotterdemarung/f13d8f462c626ba7c18179a37424c75e to your computer and use it in GitHub Desktop.
Save gotterdemarung/f13d8f462c626ba7c18179a37424c75e to your computer and use it in GitHub Desktop.
Compare-and-swap VS mutex
package util
import (
"sync"
"sync/atomic"
"testing"
)
type cas struct {
value int64
}
func (c *cas) Add(i int64) {
repeat := true
var old int64
for repeat {
old = c.value
repeat = !atomic.CompareAndSwapInt64(&c.value, old, old+i)
}
}
func (c *cas) Value() int64 {
return c.value
}
type mutex struct {
m sync.Mutex
value int64
}
func (c *mutex) Add(i int64) {
c.m.Lock()
c.value += i
c.m.Unlock()
}
func (c *mutex) Value() int64 {
return c.value
}
func bench(b *testing.B, routines int, cnt interface {
Add(int64)
Value() int64
}) {
wg := sync.WaitGroup{}
wg.Add(routines)
for i := 0; i < routines; i++ {
go func() {
for i := 0; i < b.N; i++ {
cnt.Add(1)
}
wg.Done()
}()
}
wg.Wait()
if int64(routines*b.N) != cnt.Value() {
b.Logf("Expected %d but got %d", routines*b.N, cnt.Value())
b.Fail()
}
}
func BenchmarkCas1(b *testing.B) {
bench(b, 1, &cas{})
}
func BenchmarkCas10(b *testing.B) {
bench(b, 10, &cas{})
}
func BenchmarkCas100(b *testing.B) {
bench(b, 100, &cas{})
}
func BenchmarkCas1000(b *testing.B) {
bench(b, 1000, &cas{})
}
func BenchmarkMutex1(b *testing.B) {
bench(b, 1, &mutex{})
}
func BenchmarkMutex10(b *testing.B) {
bench(b, 10, &mutex{})
}
func BenchmarkMutex100(b *testing.B) {
bench(b, 100, &mutex{})
}
func BenchmarkMutex1000(b *testing.B) {
bench(b, 1000, &mutex{})
}
// Results are:
//
// BenchmarkCas1-8 100000000 13.4 ns/op
// BenchmarkCas10-8 1000000 1483 ns/op
// BenchmarkCas100-8 100000 15414 ns/op
// BenchmarkCas1000-8 10000 156046 ns/op
// BenchmarkMutex1-8 50000000 27.2 ns/op
// BenchmarkMutex10-8 1000000 1317 ns/op
// BenchmarkMutex100-8 100000 14040 ns/op
// BenchmarkMutex1000-8 10000 160975 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment