Skip to content

Instantly share code, notes, and snippets.

@ryanuber
Last active August 19, 2016 20:42
Show Gist options
  • Save ryanuber/4726e6bcf848c41951164e68011a3b2a to your computer and use it in GitHub Desktop.
Save ryanuber/4726e6bcf848c41951164e68011a3b2a to your computer and use it in GitHub Desktop.
package locktest
import (
"sync"
"testing"
)
var l sync.Mutex
func dLock() {
l.Lock()
defer l.Unlock()
}
func lock() {
l.Lock()
l.Unlock()
}
func BenchmarkLock(b *testing.B) {
for n := 0; n < b.N; n++ {
lock()
}
}
func BenchmarkDeferLock(b *testing.B) {
for n := 0; n < b.N; n++ {
dLock()
}
}
PASS
BenchmarkLock-4 100000000 20.7 ns/op
BenchmarkDeferLock-4 20000000 93.2 ns/op
ok _/Users/ryanuber/Desktop 4.055s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment