Skip to content

Instantly share code, notes, and snippets.

@nicklanng
Created February 20, 2019 14:00
Show Gist options
  • Save nicklanng/eff7cfc08ae8f472ccf1e3ee0ece887b to your computer and use it in GitHub Desktop.
Save nicklanng/eff7cfc08ae8f472ccf1e3ee0ece887b to your computer and use it in GitHub Desktop.
Interfaces vs Function Composition
package my_test
import "testing"
type AnInterface interface {
Update() int
}
type ImplementsInterface struct {
}
func (a *ImplementsInterface) Update() int {
return 0
}
func BenchmarkAnInterface(b *testing.B) {
var target AnInterface
target = &ImplementsInterface{}
for i := 0; i < b.N; i++ {
Run(target)
}
}
func Run(target AnInterface) {
target.Update()
}
type ComposedOfFunctions struct {
Update func() int
}
func BenchmarkComposedOfFunctions(b *testing.B) {
target := ComposedOfFunctions{
Update: func() int {
return 0
},
}
for i := 0; i < b.N; i++ {
RunFunc(target)
}
}
func RunFunc(target ComposedOfFunctions) {
target.Update()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment