Skip to content

Instantly share code, notes, and snippets.

@cjlucas
Created May 7, 2015 03:29
Show Gist options
  • Save cjlucas/051fc83156d85a5ef133 to your computer and use it in GitHub Desktop.
Save cjlucas/051fc83156d85a5ef133 to your computer and use it in GitHub Desktop.
Channels are slow. Goroutines are fast.
func BenchmarkSingleGoroutine(b *testing.B) {
in := make(chan struct{})
out := make(chan struct{})
go func(in, out chan struct{}) {
for {
b := <-in
out <- b
}
}(in, out)
for i := 0; i < b.N; i++ {
in <- struct{}{}
<-out
}
}
func BenchmarkSingleChan(b *testing.B) {
done := make(chan struct{})
blah := func(done chan struct{}) {
done <- struct{}{}
}
for i := 0; i < b.N; i++ {
go blah(done)
<-done
}
}
// Results
// BenchmarkSingleGoroutine 3000000 479 ns/op
// BenchmarkSingleChan 5000000 396 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment