Skip to content

Instantly share code, notes, and snippets.

@ukautz
Created July 9, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukautz/b3d0fcb983bdf73316e53a360706dfed to your computer and use it in GitHub Desktop.
Save ukautz/b3d0fcb983bdf73316e53a360706dfed to your computer and use it in GitHub Desktop.
Benchmark channel usage with struct values vs struct references
package play
import "testing"
type (
aDataType struct {
A string
B int
C []byte
}
)
func channelViaValue() {
c := make(chan aDataType)
go func() {
defer close(c)
for i := 0; i < 100000; i++ {
c <- aDataType{"A", i, []byte{'C'}}
}
}()
for range c {
}
}
func channelViaRef() {
c := make(chan *aDataType)
go func() {
defer close(c)
for i := 0; i < 100000; i++ {
c <- &aDataType{"A", i, []byte{'C'}}
}
}()
for range c {
}
}
func BenchmarkChannelViaValue(b *testing.B) {
for n := 0; n < b.N; n++ {
channelViaValue()
}
}
func BenchmarkChannelViaRef(b *testing.B) {
for n := 0; n < b.N; n++ {
channelViaRef()
}
}
$ go test -bench=. -benchtime=10s ⛅ !EXPIRED! s24-image-scaling
goos: linux
goarch: amd64
pkg: github.com/Scout24/image-delivery-architecture-review/src/play
BenchmarkChannelViaValue-4 1000 21246004 ns/op
BenchmarkChannelViaRef-4 500 28882823 ns/op
PASS
ok github.com/Scout24/image-delivery-architecture-review/src/play 40.609s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment