Created
May 7, 2015 03:29
-
-
Save cjlucas/051fc83156d85a5ef133 to your computer and use it in GitHub Desktop.
Channels are slow. Goroutines are fast.
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
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