func BenchmarkContextSwitch(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(2)
begC := make(chan struct{})
sigC := make(chan struct{})
go func() {
<-begC
for i := 0; i < b.N; i++ {
sigC <- struct{}{}
}
wg.Done()
}()
go func() {
<-begC
for i := 0; i < b.N; i++ {
<-sigC
}
wg.Done()
}()
b.StartTimer()
close(begC)
wg.Wait()
}
λ go test -bench=BenchmarkContextSwitch -cpu=1 ctx_switch_test.go
goos: windows
goarch: amd64
BenchmarkContextSwitch 10000000 182 ns/op
PASS
ok command-line-arguments 2.045s
좋구나야~
댓글 0