forked from tendermint/classic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreflect_bench_test.go
142 lines (124 loc) · 3.73 KB
/
reflect_bench_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"reflect"
"testing"
)
type Bar struct {
B int
C int
A string
}
type Foo struct {
B int // 0
C string // 1
D int64 // 2
E int // 3
X *Bar // 4
Y Bar // 5
Bar // 6
}
//----------------------------------------
// 1
func BenchmarkReflectFieldIndex(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
rv := reflect.ValueOf(f)
t := int(0)
for i := 0; i < b.N; i++ {
frv := rv.Field(5)
frv = frv.Field(2)
t += frv.Len()
}
fmt.Println(t)
}
func BenchmarkReflectFieldName(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
rv := reflect.ValueOf(f)
t := int(0)
for i := 0; i < b.N; i++ {
frv := rv.FieldByName("Y")
frv = frv.FieldByName("A")
t += frv.Len()
}
fmt.Println(t)
}
func GetA(f Foo) string {
return string(f.Y.A)
}
func BenchmarkFunctionField(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
t := int(0)
for i := 0; i < b.N; i++ {
t += len(GetA(f))
}
fmt.Println(t)
}
//----------------------------------------
// 2
func BenchmarkReflectFieldIndex2(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
rv := reflect.ValueOf(f)
t := int(0)
for i := 0; i < b.N; i++ {
frv := rv.Field(6)
frv = frv.Field(2)
t += frv.Len()
}
fmt.Println(t)
}
func BenchmarkReflectFieldName2(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
rv := reflect.ValueOf(f)
t := int(0)
for i := 0; i < b.N; i++ {
frv := rv.FieldByName("A")
t += frv.Len()
}
fmt.Println(t)
}
func GetA2(f Foo) string {
return string(f.A)
}
func BenchmarkFunctionField2(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
t := int(0)
for i := 0; i < b.N; i++ {
t += len(GetA2(f))
}
fmt.Println(t)
}
//----------------------------------------
// 3
func BenchmarkReflectFieldIndex3(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
rv := reflect.ValueOf(f)
t := int(0)
for i := 0; i < b.N; i++ {
frv := rv.Field(4)
frv = frv.Elem().Field(2)
t += frv.Len()
}
fmt.Println(t)
}
func BenchmarkReflectFieldName3(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
rv := reflect.ValueOf(f)
t := int(0)
for i := 0; i < b.N; i++ {
frv := rv.FieldByName("X")
frv = frv.Elem().FieldByName("A")
t += frv.Len()
}
fmt.Println(t)
}
func GetA3(f Foo) string {
return string(f.X.A)
}
func BenchmarkFunctionField3(b *testing.B) {
f := Foo{X: &Bar{A: "thisissomerandomstringthatiskindalongenough"}, Y: Bar{A: "thisissomerandomstringthatiskindalongenough"}, Bar: Bar{A: "thisissomerandomstringthatiskindalongenough"}}
t := int(0)
for i := 0; i < b.N; i++ {
t += len(GetA3(f))
}
fmt.Println(t)
}