Skip to content

Instantly share code, notes, and snippets.

@dedalqq
Last active September 29, 2022 09:34
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 dedalqq/2b37ff3bd48ca85b1e5cd26e302ebfcc to your computer and use it in GitHub Desktop.
Save dedalqq/2b37ff3bd48ca85b1e5cd26e302ebfcc to your computer and use it in GitHub Desktop.

Result

Worst result

% go test -bench=. ./...
goos: linux
goarch: amd64
pkg: test/cmd/test13
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkFStruct-16                     1000000000               0.4255 ns/op
BenchmarkFStructNoinline-16             433233346                2.499 ns/op
BenchmarkFInterface-16                  456309434                2.496 ns/op
BenchmarkFInterfaceNoinline-16          246691125                4.415 ns/op
BenchmarkFGeneric-16                    259775330                4.417 ns/op
BenchmarkFGenericNoinline-16            272950497                4.005 ns/op
PASS
ok      test/cmd/test13 7.996s
go test -bench=. ./...  8.56s user 0.64s system 110% cpu 8.313 total

Best result

% go test -bench=. ./...
goos: linux
goarch: amd64
pkg: test/cmd/test13
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkFStruct-16                     1000000000               0.4528 ns/op
BenchmarkFStructNoinline-16             457676677                2.491 ns/op
BenchmarkFInterface-16                  452713634                2.418 ns/op
BenchmarkFInterfaceNoinline-16          257723593                4.632 ns/op
BenchmarkFGeneric-16                    252807128                4.583 ns/op
BenchmarkFGenericNoinline-16            466000674                2.533 ns/op
PASS
ok      test/cmd/test13 10.075s
go test -bench=. ./...  10.56s user 0.54s system 107% cpu 10.332 total
package main
import (
"testing"
)
type bar struct {
}
func (b *bar) foo() {}
type ib interface {
foo()
}
func fStruct(b *bar) {
b.foo()
}
//go:noinline
func fStructNoinline(b *bar) {
b.foo()
}
func fInterface(b ib) {
b.foo()
}
//go:noinline
func fInterfaceNoinline(b ib) {
b.foo()
}
func fGeneric[T ib](b T) {
b.foo()
}
//go:noinline
func fGenericNoinline[T ib](b T) {
b.foo()
}
func BenchmarkFStruct(b *testing.B) {
var bb bar
for n := 0; n < b.N; n++ {
fStruct(&bb)
}
}
func BenchmarkFStructNoinline(b *testing.B) {
var bb bar
for n := 0; n < b.N; n++ {
fStructNoinline(&bb)
}
}
func BenchmarkFInterface(b *testing.B) {
var bb bar
for n := 0; n < b.N; n++ {
fInterface(&bb)
}
}
func BenchmarkFInterfaceNoinline(b *testing.B) {
var bb bar
for n := 0; n < b.N; n++ {
fInterfaceNoinline(&bb)
}
}
func BenchmarkFGeneric(b *testing.B) {
var bb bar
for n := 0; n < b.N; n++ {
fGeneric(&bb)
}
}
func BenchmarkFGenericNoinline(b *testing.B) {
var bb bar
for n := 0; n < b.N; n++ {
fGenericNoinline(&bb)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment