Skip to content

Instantly share code, notes, and snippets.

@tomoemon
Created January 29, 2021 07:07
Show Gist options
  • Save tomoemon/f7fa8d9b42cc1bc326fdef2e465b1429 to your computer and use it in GitHub Desktop.
Save tomoemon/f7fa8d9b42cc1bc326fdef2e465b1429 to your computer and use it in GitHub Desktop.
package main
import "testing"
type I interface {
Hello()
}
type A struct {}
func (a A) Hello() {}
func BenchmarkStruct(b *testing.B) {
var a = A{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.Hello()
}
}
func BenchmarkInterface(b *testing.B) {
var a I = A{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.Hello()
}
}
func BenchmarkEmptyInterface(b *testing.B) {
var a = A{}
var ai interface{} = a
b.ResetTimer()
for i := 0; i < b.N; i++ {
if x, ok := ai.(A); ok {
x.Hello()
}
}
}
/*
BenchmarkStruct-8 2000000000 0.41 ns/op
BenchmarkInterface-8 500000000 3.39 ns/op
BenchmarkEmptyInterface-8 2000000000 0.36 ns/op
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment