Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

calebdoxsey commented Dec 30, 2019

† 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
You can’t perform that action at this time.