Created
February 20, 2019 14:00
-
-
Save nicklanng/eff7cfc08ae8f472ccf1e3ee0ece887b to your computer and use it in GitHub Desktop.
Interfaces vs Function Composition
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
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