-
Notifications
You must be signed in to change notification settings - Fork 24
/
benchmark_test.go
66 lines (55 loc) · 1.07 KB
/
benchmark_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
package reflect_test
import (
"reflect"
"testing"
goreflect "github.com/goccy/go-reflect"
)
func kindFromReflect(v interface{}) reflect.Kind {
return reflect.TypeOf(v).Kind()
}
func kindFromGoReflect(v interface{}) goreflect.Kind {
return goreflect.TypeOf(v).Kind()
}
func f(_ interface{}) {}
func valueFromReflect(v interface{}) {
f(reflect.ValueOf(v).Elem())
}
func valueFromGoReflect(v interface{}) {
f(goreflect.ValueNoEscapeOf(v).Elem())
}
func Benchmark_TypeOf_Reflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
var v struct {
i int
}
kindFromReflect(&v)
}
}
func Benchmark_TypeOf_GoReflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
var v struct {
i int
}
kindFromGoReflect(&v)
}
}
func Benchmark_ValueOf_Reflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
valueFromReflect(&struct {
I int
F float64
}{I: 10})
}
}
func Benchmark_ValueOf_GoReflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
valueFromGoReflect(&struct {
I int
F float64
}{I: 10})
}
}