We have heard many complaining that channels are slow. But when I tried to benchmark this, we get some surprising results. A queue seems slower than a channel?!
The results read:
BenchmarkChan-8 20000000 67.0 ns/op
BenchmarkQueue-8 10000000 172 ns/op
BenchmarkCQueue-8 5000000 307 ns/op
BenchmarkGringo-8 20000000 81.8 ns/op
Maybe someone can take a look at the code to see if I implement these queues optimally. Queue
wraps around a Go array that keeps growing indefinitely. CQueue
uses the Go array as a circular buffer.
The testing code looks like this:
func BenchmarkChan(b *testing.B) {
c := make(chan struct{}, 10)
b.StartTimer()
go func() {
for i := 0; i < b.N; i++ {
c <- struct{}{}
}
close(c)
}()
for _ = range c {
}
}