Skip to content

Instantly share code, notes, and snippets.

@embano1
Last active April 22, 2022 09:01
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 embano1/445fd8f01d1ef15d3bb40f29b5214738 to your computer and use it in GitHub Desktop.
Save embano1/445fd8f01d1ef15d3bb40f29b5214738 to your computer and use it in GitHub Desktop.
go 1.18 assert.Equal benchmark
package assert
func EqualGeneric[T comparable](expected, actual T) bool {
return expected == actual
}
func EqualInterface(expected, actual interface{}) bool {
return expected == actual
}
package assert
import (
"testing"
)
func BenchmarkEqualGeneric(b *testing.B) {
for n := 0; n < b.N; n++ {
EqualGeneric(10, 10)
}
}
func BenchmarkEqualInterface(b *testing.B) {
for n := 0; n < b.N; n++ {
EqualInterface(10, 10)
}
}
go test -v -bench=. -gcflags '-l -N' -cpu=1,2,4 -benchmem
goos: darwin
goarch: amd64
pkg: github.com/embano1/assertbench
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkEqualGeneric
BenchmarkEqualGeneric           462083778                2.392 ns/op           0 B/op          0 allocs/op
BenchmarkEqualGeneric-2         504194739                2.391 ns/op           0 B/op          0 allocs/op
BenchmarkEqualGeneric-4         513227227                2.470 ns/op           0 B/op          0 allocs/op
BenchmarkEqualInterface
BenchmarkEqualInterface         217360456                5.366 ns/op           0 B/op          0 allocs/op
BenchmarkEqualInterface-2       218655147                5.473 ns/op           0 B/op          0 allocs/op
BenchmarkEqualInterface-4       219182017                5.470 ns/op           0 B/op          0 allocs/op
PASS
ok      github.com/embano1/assertbench  9.874s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment