Skip to content

Instantly share code, notes, and snippets.

@atotto
Created March 4, 2014 09:13
Show Gist options
  • Save atotto/9342938 to your computer and use it in GitHub Desktop.
Save atotto/9342938 to your computer and use it in GitHub Desktop.
golang channel benchmark
package chan_test
import (
"testing"
)
func BenchmarkStructChan(b *testing.B) {
ch := make(chan struct{})
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- struct{}{}
}
}
func BenchmarkBoolChan(b *testing.B) {
ch := make(chan bool)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- true
}
}
func BenchmarkIntChan(b *testing.B) {
ch := make(chan int)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- 1
}
}
@sameer
Copy link

sameer commented Nov 23, 2017

To add onto that, if you were really trying to send one item at a time, you should be using a mutex from sync.

Here's what I got on a 2 vCPU (Haswell 3GHz) VPS with 1.6.2 and 1.9.2.

$ go version
go version go1.6.2 linux/amd64
$ go test -test.bench Bench
testing: warning: no tests to run
PASS
BenchmarkStructChan-2	 5000000	       260 ns/op
BenchmarkBoolChan-2  	 5000000	       283 ns/op
BenchmarkIntChan-2   	 5000000	       264 ns/op
ok  	_/root/test	4.948s

$ go version
go version go1.9.2 linux/amd64
$ go test -test.bench Bench
goos: linux
goarch: amd64
BenchmarkStructChan-2   	10000000	       258 ns/op
BenchmarkBoolChan-2     	 5000000	       243 ns/op
BenchmarkIntChan-2      	10000000	       234 ns/op
PASS
ok  	_/root/test	5.650s

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