Skip to content

Instantly share code, notes, and snippets.

@codegangsta
Created November 27, 2013 15:55
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 codegangsta/7678025 to your computer and use it in GitHub Desktop.
Save codegangsta/7678025 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/codegangsta/inject"
"reflect"
"testing"
)
func helloWorld(val string) {
i := 0
i++
}
func BenchmarkFunctionCall(b *testing.B) {
for i := 0; i < b.N; i++ {
helloWorld("foo")
}
}
func BenchmarkReflectCall(b *testing.B) {
val := reflect.ValueOf(helloWorld)
for i := 0; i < b.N; i++ {
val.Call([]reflect.Value{reflect.ValueOf("foo")})
}
}
func BenchmarkInvoke(b *testing.B) {
injector := inject.New()
injector.Map("foo")
for i := 0; i < b.N; i++ {
injector.Invoke(helloWorld)
}
}
@codegangsta
Copy link
Author

BenchmarkFunctionCall 2000000000 0.57 ns/op
BenchmarkReflectCall 5000000 380 ns/op
BenchmarkInvoke 5000000 538 ns/op

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