Skip to content

Instantly share code, notes, and snippets.

@chobie
Created September 24, 2014 15:49
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 chobie/5f3fee3ecd3fb05b5891 to your computer and use it in GitHub Desktop.
Save chobie/5f3fee3ecd3fb05b5891 to your computer and use it in GitHub Desktop.
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
}
}
@chobie
Copy link
Author

chobie commented Sep 24, 2014

BenchmarkLock 50000000 44.5 ns/op
BenchmarkChannel 5000000 324 ns/op
BenchmarkChannel2 20000000 106 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment