Skip to content

Instantly share code, notes, and snippets.

@sioncojp
Created March 22, 2017 07:42
Show Gist options
  • Save sioncojp/c4252bc43d30adefff8d6c51f363c63e to your computer and use it in GitHub Desktop.
Save sioncojp/c4252bc43d30adefff8d6c51f363c63e to your computer and use it in GitHub Desktop.
channelを作成する際に、capacityをつける場合とつけない場合のbenchmark
package main
import "testing"
func BenchmarkChan1(b *testing.B) {
ch := make(chan int)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- 1
}
}
func BenchmarkChan2(b *testing.B) {
ch := make(chan int, 1)
go func() {
for {
<-ch
}
}()
for i := 0; i < b.N; i++ {
ch <- 1
}
}
// go test -bench .
// BenchmarkChan1-8 10000000 240 ns/op
// BenchmarkChan2-8 10000000 173 ns/op
// PASS
// ok my 4.572s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment