Last active
October 2, 2015 03:13
-
-
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
This file contains hidden or 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
//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