-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
152 lines (131 loc) · 2.79 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
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
143
144
145
146
147
148
149
150
151
152
package govert
import (
"reflect"
"strconv"
"testing"
)
var (
i int = 101
ii interface{} = int(101)
s string = "25.41"
si interface{} = "25.41"
tmp interface{}
)
func e(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
}
/* =========================================================================
* converting from int
* =========================================================================
*/
func Benchmark_InterfaceInt_ToString_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = String(ii)
}
}
func Benchmark_InterfaceInt_ToFloat64_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = Float64(ii)
}
}
func Benchmark_Int_ToString_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = String(i)
}
}
func Benchmark_Int_ToFloat64_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = String(i)
}
}
func Benchmark_InterfaceInt_ToString_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
if reflect.TypeOf(ii).Kind() == reflect.Int {
tmp = strconv.FormatInt(int64(ii.(int)), 10)
}
}
}
func Benchmark_InterfaceInt_ToFloat64_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
if reflect.TypeOf(ii).Kind() == reflect.Int {
tmp = float64(ii.(int))
}
}
}
func Benchmark_Int_ToString_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = strconv.FormatInt(int64(i), 10)
}
}
func Benchmark_Int_ToFloat64_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = float64(i)
}
}
/* =========================================================================
* converting from string
* =========================================================================
*/
func Benchmark_InterfaceString_ToInt_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = Int(si)
}
}
func Benchmark_InterfaceString_ToFloat64_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = Float64(si)
}
}
func Benchmark_String_ToInt_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = Int(s)
}
}
func Benchmark_String_ToFloat64_govert(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp = Int(s)
}
}
func Benchmark_InterfaceString_ToInt_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
if reflect.TypeOf(si).Kind() == reflect.String {
t, _ := strconv.ParseInt(si.(string), 10, 64)
tmp = int(t)
}
}
}
func Benchmark_InterfaceString_ToFloat64_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
if reflect.TypeOf(si).Kind() == reflect.String {
tmp, _ = strconv.ParseFloat(si.(string), 64)
}
}
}
func Benchmark_String_ToInt_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
t1, _ := strconv.ParseInt(s, 10, 64)
tmp = int(t1)
}
}
func Benchmark_String_ToFloat64_manual(b *testing.B) {
e(b)
for n := 0; n < b.N; n++ {
tmp, _ = strconv.ParseFloat(s, 64)
}
}