tins2020/conversion_test.go

43 lines
917 B
Go

package tins2020_test
import (
"math/rand"
"testing"
)
type native struct{ a, b, c, d int }
type similar struct{ a, b, c, d int }
func (s similar) toNative() native { return native{s.a, s.b, s.c, s.d} }
type wrapper struct{ native }
func (w wrapper) toNative() native { return w.native }
func nativeFunction(n native) int { return n.a + n.b + n.c + n.d }
func BenchmarkNative(b *testing.B) {
var sum int
for i := 0; i < b.N; i++ {
n := native{rand.Int(), rand.Int(), rand.Int(), rand.Int()}
sum += nativeFunction(n)
}
}
func BenchmarkSimilar(b *testing.B) {
var sum int
for i := 0; i < b.N; i++ {
s := similar{rand.Int(), rand.Int(), rand.Int(), rand.Int()}
sum += nativeFunction(s.toNative())
}
}
func BenchmarkWrapper(b *testing.B) {
var sum int
for i := 0; i < b.N; i++ {
w := wrapper{native{rand.Int(), rand.Int(), rand.Int(), rand.Int()}}
sum += nativeFunction(w.toNative())
}
}