Skip to content

Instantly share code, notes, and snippets.

@francoishill
Last active October 2, 2015 03:13
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 francoishill/4bdb47bf4c99a0a3e65e to your computer and use it in GitHub Desktop.
Save francoishill/4bdb47bf4c99a0a3e65e to your computer and use it in GitHub Desktop.
The idea of this benchmark is to determine the performance cost of when we pass around interfaces instead of the concrete struct and getting a field on the struct vs calling a `get` method on the interface
//The idea of this benchmark is to determine the
// performance cost of when we pass around interfaces
// instead of the concrete struct and getting a field
// on the struct vs calling a `get` method on the
// interface
package test
import (
"testing"
)
var (
globalInt64 int64
usr *user = &user{25}
iUser User = usr
)
type User interface {
GetId() int64
}
type user struct {
Id int64
}
func (u *user) GetId() int64 {
return u.Id
}
func BenchmarkStructField(b *testing.B) {
var g int64
for n := 0; n < b.N; n++ {
g = usr.Id
if g > 24 {
g = 0
}
}
globalInt64 = g
}
func BenchmarkInterfaceMethod(b *testing.B) {
var g int64
for n := 0; n < b.N; n++ {
g = iUser.GetId()
if g > 24 {
g = 0
}
}
globalInt64 = g
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment