Skip to content

Instantly share code, notes, and snippets.

@jakschu
Forked from TheHippo/main.go
Last active August 29, 2015 14:27
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 jakschu/22d10d55a04b6dcf5347 to your computer and use it in GitHub Desktop.
Save jakschu/22d10d55a04b6dcf5347 to your computer and use it in GitHub Desktop.
Benchmarking Go Mutex overhead
package main
import (
"fmt"
"sync"
)
type unlocked struct {
i int
}
type locked struct {
i int
l sync.Mutex
}
func (c *unlocked) add() {
c.i++
}
func (c *locked) add() {
c.l.Lock()
defer c.l.Unlock()
c.i++
}
func main() {
fmt.Println("ok")
}
package main
import "testing"
func TestLocked_add(t *testing.T) {
c := locked{}
c.add()
if c.i != 1 {
t.Errorf("Expected 1 but got %d", c.i)
}
}
func BenchmarkLocked_add(t *testing.B) {
c := locked{}
for i := 0; i < t.N; i++ {
c.add()
}
if c.i != t.N {
t.Errorf("Expected %d, but got %d", t.N, c.i)
}
}
func TestUnlocked_add(t *testing.T) {
c := unlocked{}
c.add()
if c.i != 1 {
t.Errorf("Expected 1 but got %d", c.i)
}
}
func BenchmarkUnlocked_add(t *testing.B) {
c := unlocked{}
for i := 0; i < t.N; i++ {
c.add()
}
if c.i != t.N {
t.Errorf("Expected %d, but got %d", t.N, c.i)
}
}
$ go test -v -bench .
=== RUN TestLocked_add
--- PASS: TestLocked_add (0.00s)
=== RUN TestUnlocked_add
--- PASS: TestUnlocked_add (0.00s)
PASS
BenchmarkLocked_add 10000000 143 ns/op
BenchmarkUnlocked_add 1000000000 2.41 ns/op
ok _/home/hippo/test/mutex-bench 4.226s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment