Created
September 24, 2014 15:49
-
-
Save chobie/5f3fee3ecd3fb05b5891 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 main | |
import ( | |
"sync" | |
"testing" | |
) | |
func BenchmarkLock(t *testing.B) { | |
mu := sync.Mutex{} | |
m := make(map[int]bool) | |
for i := 0; i < t.N; i++ { | |
v := true | |
mu.Lock() | |
m[1] = v | |
mu.Unlock() | |
} | |
} | |
func BenchmarkChannel(t *testing.B) { | |
m := make(map[int]bool) | |
c := make(chan bool, 1) | |
go func() { | |
for x := range c { | |
m[1] = x | |
} | |
}() | |
for i := 0; i < t.N; i++ { | |
v := true | |
c <- v | |
} | |
} | |
func BenchmarkChannel2(t *testing.B) { | |
m := make(map[int]bool) | |
c := make(chan bool, 8192) | |
go func() { | |
for x := range c { | |
m[1] = x | |
} | |
}() | |
for i := 0; i < t.N; i++ { | |
v := true | |
c <- v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BenchmarkLock 50000000 44.5 ns/op
BenchmarkChannel 5000000 324 ns/op
BenchmarkChannel2 20000000 106 ns/op