Created
September 17, 2020 10:26
-
-
Save yankooo/1bd4ea9c294bda3054d75b306a382e01 to your computer and use it in GitHub Desktop.
channel.go
This file contains hidden or 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 garbage | |
import "testing" | |
var i int | |
func BenchmarkBoolInt(b *testing.B) { | |
c := make(chan int, b.N) | |
for i := 0; i < b.N; i++ { | |
c <- i | |
} | |
close(c) | |
for range c { | |
i = <-c | |
} | |
} | |
var bl bool | |
func BenchmarkBoolChan(b *testing.B) { | |
c := make(chan bool, b.N) | |
for i := 0; i < b.N; i++ { | |
c <- true | |
} | |
close(c) | |
for range c { | |
bl = <-c | |
} | |
} | |
var s struct{} | |
func BenchmarkStructChan(b *testing.B) { | |
c := make(chan struct{}, b.N) | |
for i := 0; i < b.N; i++ { | |
c <- struct{}{} | |
} | |
close(c) | |
for range c { | |
s = <-c | |
} | |
} | |
var p *struct{} | |
func BenchmarkStructPtrChan(b *testing.B) { | |
c := make(chan *struct{}, b.N) | |
for i := 0; i < b.N; i++ { | |
c <- nil | |
} | |
close(c) | |
for range c { | |
p = <-c | |
} | |
} | |
/* | |
➜ garbage go test -v -bench=Benchmark -test.benchmem -cpu=1,8,16,32 | |
goos: darwin | |
goarch: amd64 | |
pkg: garbage | |
BenchmarkBoolInt 30000000 48.4 ns/op 8 B/op 0 allocs/op | |
BenchmarkBoolInt-8 30000000 48.8 ns/op 8 B/op 0 allocs/op | |
BenchmarkBoolInt-16 30000000 46.3 ns/op 8 B/op 0 allocs/op | |
BenchmarkBoolInt-32 30000000 44.6 ns/op 8 B/op 0 allocs/op | |
BenchmarkBoolChan 30000000 43.4 ns/op 1 B/op 0 allocs/op | |
BenchmarkBoolChan-8 30000000 43.1 ns/op 1 B/op 0 allocs/op | |
BenchmarkBoolChan-16 30000000 42.9 ns/op 1 B/op 0 allocs/op | |
BenchmarkBoolChan-32 30000000 44.3 ns/op 1 B/op 0 allocs/op | |
BenchmarkStructChan 30000000 44.7 ns/op 0 B/op 0 allocs/op | |
BenchmarkStructChan-8 30000000 43.3 ns/op 0 B/op 0 allocs/op | |
BenchmarkStructChan-16 30000000 44.9 ns/op 0 B/op 0 allocs/op | |
BenchmarkStructChan-32 30000000 43.3 ns/op 0 B/op 0 allocs/op | |
BenchmarkStructPtrChan 20000000 53.7 ns/op 8 B/op 0 allocs/op | |
BenchmarkStructPtrChan-8 30000000 51.0 ns/op 8 B/op 0 allocs/op | |
BenchmarkStructPtrChan-16 30000000 51.6 ns/op 8 B/op 0 allocs/op | |
BenchmarkStructPtrChan-32 30000000 51.1 ns/op 8 B/op 0 allocs/op | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment