-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
reflect_test.go
65 lines (55 loc) · 1.17 KB
/
reflect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package gocostmodel
import (
"image"
"reflect"
"testing"
)
type reflectStr struct {
a1 int
}
var (
reflectStra = reflectStr{}
reflectStrp = &reflectStr{}
reflectStrVal = reflect.ValueOf(reflectStr{1})
reflectStrPVal = reflect.ValueOf(&reflectStr{1})
fld reflect.StructField
pt = image.Point{}
)
func BenchmarkReflFromVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStra = reflectStrVal.Interface().(reflectStr)
}
}
func BenchmarkReflToVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStrVal = reflect.ValueOf(reflectStra)
}
}
func BenchmarkReflPtrFromVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStrp = reflectStrPVal.Interface().(*reflectStr)
}
}
func BenchmarkReflPtrToVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStrPVal = reflect.ValueOf(reflectStrp)
}
}
func BenchmarkReflSmallStructFields(b *testing.B) {
for i := 0; i < b.N; i++ {
t := reflect.TypeOf(pt)
n := t.NumField()
for j := 0; j < n; j++ {
fld = t.Field(j)
}
}
}
func BenchmarkReflLargeStructFields(b *testing.B) {
for i := 0; i < b.N; i++ {
t := reflect.TypeOf(req)
n := t.NumField()
for j := 0; j < n; j++ {
fld = t.Field(j)
}
}
}