Comparable outcomes have been obtained for various platforms and architectures.
It’s not a language downside, is it?
“
sort Signed interface ~int32
sort Getter interface {
Get(int) int
}
sort GetObj struct {}
func (o *GetObj) Get(i int) int {
return i * i
}
sort GenGetter[T Signed] interface {
Get(T) T
}
sort GenGetObj[T Signed] struct {}
func (o *GenGetObj[T]) Get(i T) T {
return i * i
}
func reg(obj *GetObj, i int) int {
return obj.Get(i)
}
func iface(obj Getter, i int) int {
return obj.Get(i)
}
func gen[T Signed](obj GenGetter[T], i T) T {
return obj.Get(i)
}
func BenchmarkReg(b *testing.B) {
var obj = &GetObj{}
var i = 0
for b.Loop() {
reg(obj, i)
i++
}
}
func BenchmarkIface(b *testing.B) {
var obj = &GetObj{}
var i = 0
for b.Loop() {
iface(obj, i)
i++
}
}
func BenchmarkGen(b *testing.B) {
var obj = &GenGetObj[int]{}
var i = 0
for b.Loop() {
gen(obj, i)
i++
}
}
BenchmarkReg-4 441981427 2.743 ns/op
BenchmarkReg-4 428439991 2.810 ns/op
BenchmarkReg-4 422647483 2.775 ns/op
BenchmarkReg-4 414114366 2.839 ns/op
BenchmarkReg-4 429933127 2.720 ns/op
BenchmarkIface-4 260450607 4.558 ns/op
BenchmarkIface-4 258018102 4.488 ns/op
BenchmarkIface-4 248518578 4.655 ns/op
BenchmarkIface-4 257133632 4.517 ns/op
BenchmarkIface-4 272843784 4.386 ns/op
BenchmarkGen-4 245088946 4.932 ns/op
BenchmarkGen-4 248562715 4.826 ns/op
BenchmarkGen-4 245683516 4.812 ns/op
BenchmarkGen-4 239086440 4.948 ns/op
BenchmarkGen-4 228736507 5.074 ns/op
What precisely did you attempt and what's your downside with the outcomes?