Created
January 29, 2021 07:07
-
-
Save tomoemon/f7fa8d9b42cc1bc326fdef2e465b1429 to your computer and use it in GitHub Desktop.
This file contains 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 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