Created
November 30, 2018 08:25
-
-
Save lysu/ff9623ce7417ef00a66d935bdc7eefb8 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 ( | |
"context" | |
"testing" | |
) | |
type Iface interface { | |
F(ctx context.Context) error | |
} | |
type IfTest struct {} | |
func (i *IfTest) F(ctx context.Context) error { | |
return nil | |
} | |
type FuncTest struct { | |
Fn func(ctx context.Context) error | |
} | |
func ee(ctx context.Context) error { | |
return nil | |
} | |
func BenchmarkCallFn(b *testing.B) { | |
ff := &FuncTest{Fn: ee} | |
c := context.Background() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
ff.Fn(c) | |
} | |
} | |
func BenchmarkCallInterface(b *testing.B) { | |
var ii Iface = &IfTest{} | |
c := context.Background() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
ii.F(c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
goos: linux
goarch: amd64
pkg: github.com/pingcap/tidb/plugin/conn_ip_example
BenchmarkCallInterface-8 1000000000 3.03 ns/op
BenchmarkCallFn-8 1000000000 2.71 ns/op
PASS