Skip to content

Instantly share code, notes, and snippets.

@himulawang
Created August 30, 2015 16:23
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 himulawang/7dc517bff5d3fee06f9b to your computer and use it in GitHub Desktop.
Save himulawang/7dc517bff5d3fee06f9b to your computer and use it in GitHub Desktop.
Go Reflect Performance
package main
import (
"fmt"
"time"
"reflect"
)
func TestReflect(a int64) {
var i int64
for i = 0; i < 10000; i++ {
a += i
}
}
func main() {
var i int64
start := time.Now().UnixNano()
for i = 0; i < 10000; i++ {
TestReflect(i)
}
end := time.Now().UnixNano()
fmt.Println("native function performance", end - start)
m := map[string]func(int64){
"TestFunc": TestReflect,
}
start = time.Now().UnixNano()
for i = 0; i < 10000; i++ {
r := reflect.ValueOf(m["TestFunc"])
r.Call([]reflect.Value{reflect.ValueOf(i)})
}
end = time.Now().UnixNano()
fmt.Println("reflect function performance", end - start)
}
// go 1.5
//native function performance 55052600
//reflect function performance 55056300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment