Created
March 6, 2019 17:09
-
-
Save apmckinlay/6f9c0aaea3f45c4af7b4d83c732a08f8 to your computer and use it in GitHub Desktop.
testing Go small structs and interfaces
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 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