Created
March 22, 2017 07:42
-
-
Save sioncojp/c4252bc43d30adefff8d6c51f363c63e to your computer and use it in GitHub Desktop.
channelを作成する際に、capacityをつける場合とつけない場合のbenchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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