# Generics benchmarks

**URL:** <https://forum.golangbridge.org/t/generics-benchmarks/38861>\
**Category:** Technical Discussion\
**Created:** [May 23, 2025, 7:30pm UTC](https://forum.golangbridge.org/t/generics-benchmarks/38861 "2025-05-23T19:30:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwr](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mwr](https://forum.golangbridge.org/u/mwr)\
**Post date:** [May 23, 2025, 7:30pm UTC](https://forum.golangbridge.org/t/generics-benchmarks/38861/1 "2025-05-23T19:30:58Z")

</div>

Similar results were obtained for different platforms and architectures.  
It’s not a language problem at all, is it?

```auto
type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64;
}

type Getter interface {
	Get(int) int
}

type GetObj struct {}

func (o *GetObj) Get(i int) int {
	return i * i
}

type GenGetter[T Signed] interface {
	Get(T) T
}

type 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++
	}
}

```

```auto
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

```

And this is probably the wrong version, but I’ll give it anyway:

```auto
func BenchmarkReg(b *testing.B) {
	var obj = &GetObj{}
	for i := range b.N {
		reg(obj, i)
	}
}

func BenchmarkIface(b *testing.B) {
	var obj = &GetObj{}
	for i := range b.N {
		iface(obj, i)
	}
}

func BenchmarkGen(b *testing.B) {
	var obj = &GenGetObj[int]{}
	for i := range b.N {
		gen(obj, i)
	}
}

```

```auto
BenchmarkReg-4 1000000000 0.3700 ns/op
BenchmarkReg-4 1000000000 0.3609 ns/op
BenchmarkReg-4 1000000000 0.3643 ns/op
BenchmarkReg-4 1000000000 0.3616 ns/op
BenchmarkReg-4 1000000000 0.3778 ns/op
BenchmarkIface-4 1000000000 0.3492 ns/op
BenchmarkIface-4 1000000000 0.3651 ns/op
BenchmarkIface-4 1000000000 0.3571 ns/op
BenchmarkIface-4 1000000000 0.3629 ns/op
BenchmarkIface-4 1000000000 0.3499 ns/op
BenchmarkGen-4 419146522 2.703 ns/op
BenchmarkGen-4 420824724 2.777 ns/op
BenchmarkGen-4 416835702 2.740 ns/op
BenchmarkGen-4 415282959 2.803 ns/op
BenchmarkGen-4 426988256 2.732 ns/op

```

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [May 27, 2025, 8:56am UTC](https://forum.golangbridge.org/t/generics-benchmarks/38861/2 "2025-05-27T08:56:29Z")

</div>

I think for these simple use-cases looking at the assembly will probably give more insights than just looking at the numbers. My first guess would be the regular call is heavily optimized by inlining and might even be optimized away completely, if you don’t use the value at all.

---

<div class="post-metadata">

**Author:** ![lemarkar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lemarkar/32/12652_2.png) [@lemarkar](https://forum.golangbridge.org/u/lemarkar)\
**Post date:** [May 27, 2025, 12:23pm UTC](https://forum.golangbridge.org/t/generics-benchmarks/38861/3 "2025-05-27T12:23:28Z")

</div>

Well, I’ve obtained different results which prove the opposite:

```auto
test-generics % go test -bench=.
goos: darwin
goarch: arm64
pkg: test-generics
cpu: Apple M1 Pro
BenchmarkReg-10 538556432 2.121 ns/op
BenchmarkIface-10 569057384 2.085 ns/op
BenchmarkGen-10 582057588 2.066 ns/op
PASS
ok test-generics 3.715s

test-generics % go test -bench=. -cpu 1
goos: darwin
goarch: arm64
pkg: test-generics
cpu: Apple M1 Pro
BenchmarkReg 511943661 2.148 ns/op
BenchmarkIface 576335656 2.070 ns/op
BenchmarkGen 574286329 2.066 ns/op
PASS
ok test-generics 3.745s

```

What is the platform? How many cores did you run? We need more details

---

<div class="post-metadata">

**Author:** ![Willie28John](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/w/e56c9b/32.png) [@Willie28John](https://forum.golangbridge.org/u/Willie28John)\
**Post date:** [June 1, 2025, 5:47pm UTC](https://forum.golangbridge.org/t/generics-benchmarks/38861/4 "2025-06-01T17:47:40Z")

</div>

Hello!  
Thanks for the info I will try to figure it out for more.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [August 30, 2025, 5:48pm UTC](https://forum.golangbridge.org/t/generics-benchmarks/38861/5 "2025-08-30T17:48:04Z")

</div>

Closed on August 30, 2025, 5:48pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
