Skip to content

Instantly share code, notes, and snippets.

@chobie chobie/lock_test.go
Created Sep 24, 2014

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.