Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created February 20, 2022 07:50
Show Gist options
  • Save percybolmer/c12749232620f431237c6e5169337ada to your computer and use it in GitHub Desktop.
Save percybolmer/c12749232620f431237c6e5169337ada to your computer and use it in GitHub Desktop.
benchmarking the generics vs type switched
package benchmarking
import "testing"
func Benchmark_Structures(b *testing.B) {
// Init the structs
p := &Person[float32]{Name: "John"}
c := &Car[int]{Name: "Ferrari"}
pRegular := &PersonRegular{Name: "John"}
cRegular := &CarRegular{Name: "Ferrari"}
// Run the test
b.Run("Person_Generic_Move", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// generic will try to use float64 if we dont tell it is a float32
Move[float32](p, 10.2)
}
})
b.Run("Car_Generic_Move", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Move(c, 10)
}
})
b.Run("Person_Regular_Move", func(b *testing.B) {
for i := 0; i < b.N; i++ {
MoveRegular(pRegular, 10.2)
}
})
b.Run("Car_Regular_Move", func(b *testing.B) {
for i := 0; i < b.N; i++ {
MoveRegular(cRegular, 10)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment