Skip to content

Instantly share code, notes, and snippets.

@apmckinlay
Created March 6, 2019 17:09
Show Gist options
  • Save apmckinlay/6f9c0aaea3f45c4af7b4d83c732a08f8 to your computer and use it in GitHub Desktop.
Save apmckinlay/6f9c0aaea3f45c4af7b4d83c732a08f8 to your computer and use it in GitHub Desktop.
testing Go small structs and interfaces
package runtime
import "testing"
type intfc interface {
f() intfc
}
type byval struct {
x int
y int
}
func (s byval) f() intfc { // by value
s.x++
return s
}
func BenchmarkSmallByValue(b *testing.B) {
var v intfc = byval{1, 1}
for n := 0; n < b.N; n++ {
v = v.f()
}
}
type byptr struct {
x int
y int
}
func (s *byptr) f() intfc { // by pointer
s.x++
return s
}
func BenchmarkSmallByPointer(b *testing.B) {
var v intfc = &byptr{1, 1}
for n := 0; n < b.N; n++ {
v = v.f()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment