Skip to content

Instantly share code, notes, and snippets.

@oherych
Created December 22, 2021 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oherych/7b4771195c4cab5dd5f748839cefcfba to your computer and use it in GitHub Desktop.
Save oherych/7b4771195c4cab5dd5f748839cefcfba to your computer and use it in GitHub Desktop.
package sssss
import "testing"
type A struct {
List []interface{}
}
func (a *A) Add(in interface{}) {
a.List = append(a.List, in)
}
type B[T any] struct {
List []T
}
func (b *B[T]) Add(in T) {
b.List = append(b.List, in)
}
func BenchmarkDemo(b *testing.B) {
const size = 1000
b.Run("Interface", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var obj A
for j:=0; j< size; j++ {
obj.Add(i)
}
}
})
b.Run("Generics", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var obj B[int]
for j:=0; j< size; j++ {
obj.Add(i)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment