Created
August 30, 2015 16:23
-
-
Save himulawang/7dc517bff5d3fee06f9b to your computer and use it in GitHub Desktop.
Go Reflect Performance
This file contains 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 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