Skip to content

Instantly share code, notes, and snippets.

@calebdoxsey
Created December 30, 2019 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebdoxsey/61967cea2bfa2047ef05d15f5b3a7ba5 to your computer and use it in GitHub Desktop.
Save calebdoxsey/61967cea2bfa2047ef05d15f5b3a7ba5 to your computer and use it in GitHub Desktop.
channel benchmark
package main
import "testing"
func BenchmarkOneByteChannel(b *testing.B) {
ch := make(chan [1]byte)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- [1]byte{}
}
}
func BenchmarkTenByteChannel(b *testing.B) {
ch := make(chan [10]byte)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- [10]byte{}
}
}
func BenchmarkHundredByteChannel(b *testing.B) {
ch := make(chan [100]byte)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- [100]byte{}
}
}
func BenchmarkThousandByteChannel(b *testing.B) {
ch := make(chan [1000]byte)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- [1000]byte{}
}
}
@calebdoxsey
Copy link
Author

† go test -bench='.*' .
goos: darwin
goarch: amd64
pkg: example/channelexample
BenchmarkOneByteChannel-4        	 2932813	       388 ns/op
BenchmarkTenByteChannel-4        	 3090325	       414 ns/op
BenchmarkHundredByteChannel-4    	 3049754	       400 ns/op
BenchmarkThousandByteChannel-4   	 2781367	       415 ns/op
PASS
ok  	example/channelexample	6.471s

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