Skip to content

Instantly share code, notes, and snippets.

@lysu
Created November 30, 2018 08:25
Show Gist options
  • Save lysu/ff9623ce7417ef00a66d935bdc7eefb8 to your computer and use it in GitHub Desktop.
Save lysu/ff9623ce7417ef00a66d935bdc7eefb8 to your computer and use it in GitHub Desktop.
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)
}
}
@lysu
Copy link
Author

lysu commented Nov 30, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment