Created
March 3, 2017 11:40
-
-
Save DavadDi/3ac7a27f11ef9c61c4fd754fcb409c23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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