Skip to content

Instantly share code, notes, and snippets.

@DavadDi
Created March 3, 2017 11:40
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 DavadDi/3ac7a27f11ef9c61c4fd754fcb409c23 to your computer and use it in GitHub Desktop.
Save DavadDi/3ac7a27f11ef9c61c4fd754fcb409c23 to your computer and use it in GitHub Desktop.
package test
import (
"sync"
"testing"
)
var m sync.Mutex
func call() {
m.Lock()
m.Unlock()
}
func deferCall() {
m.Lock()
defer m.Unlock()
}
func BenchmarkCall(b *testing.B) {
for i := 0; i < b.N; i++ {
call()
}
}
func BenchmarkDeferCall(b *testing.B) {
for i := 0; i < b.N; i++ {
deferCall()
}
}
/*
BenchmarkCall-8 100000000 22.2 ns/op
BenchmarkDeferCall-8 10000000 118 ns/op
PASS
ok defer_perf 3.557s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment