Created
December 22, 2021 20:20
-
-
Save oherych/9bbe4ca995b96bbe541a6fdded4f7661 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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