forked from shopspring/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_bench_test.go
314 lines (272 loc) · 5.91 KB
/
decimal_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package decimal
import (
"fmt"
"math"
"math/big"
"math/rand"
"sort"
"strconv"
"testing"
)
type DecimalSlice []Decimal
func (p DecimalSlice) Len() int { return len(p) }
func (p DecimalSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p DecimalSlice) Less(i, j int) bool { return p[i].Cmp(p[j]) < 0 }
func BenchmarkNewFromFloatWithExponent(b *testing.B) {
rng := rand.New(rand.NewSource(0xdead1337))
in := make([]float64, b.N)
for i := range in {
in[i] = rng.NormFloat64() * 10e20
}
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
in := rng.NormFloat64() * 10e20
_ = NewFromFloatWithExponent(in, math.MinInt32)
}
}
func BenchmarkNewFromFloat(b *testing.B) {
rng := rand.New(rand.NewSource(0xdead1337))
in := make([]float64, b.N)
for i := range in {
in[i] = rng.NormFloat64() * 10e20
}
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = NewFromFloat(in[i])
}
}
func BenchmarkNewFromStringFloat(b *testing.B) {
rng := rand.New(rand.NewSource(0xdead1337))
in := make([]float64, b.N)
for i := range in {
in[i] = rng.NormFloat64() * 10e20
}
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
in := strconv.FormatFloat(in[i], 'f', -1, 64)
_, _ = NewFromString(in)
}
}
func Benchmark_FloorFast(b *testing.B) {
input := New(200, 2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
input.Floor()
}
}
func Benchmark_FloorRegular(b *testing.B) {
input := New(200, -2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
input.Floor()
}
}
func Benchmark_DivideOriginal(b *testing.B) {
tcs := createDivTestCases()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, tc := range tcs {
d := tc.d
if sign(tc.d2) == 0 {
continue
}
d2 := tc.d2
prec := tc.prec
a := d.DivOld(d2, int(prec))
if sign(a) > 2 {
panic("dummy panic")
}
}
}
}
func Benchmark_DivideNew(b *testing.B) {
tcs := createDivTestCases()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, tc := range tcs {
d := tc.d
if sign(tc.d2) == 0 {
continue
}
d2 := tc.d2
prec := tc.prec
a := d.DivRound(d2, prec)
if sign(a) > 2 {
panic("dummy panic")
}
}
}
}
func BenchmarkDecimal_RoundCash_Five(b *testing.B) {
const want = "3.50"
for i := 0; i < b.N; i++ {
val := New(3478, -3)
if have := val.StringFixedCash(5); have != want {
b.Fatalf("\nHave: %q\nWant: %q", have, want)
}
}
}
func numDigits(b *testing.B, want int, val Decimal) {
b.Helper()
for i := 0; i < b.N; i++ {
if have := val.NumDigits(); have != want {
b.Fatalf("\nHave: %d\nWant: %d", have, want)
}
}
}
func BenchmarkDecimal_NumDigits10(b *testing.B) {
numDigits(b, 10, New(3478512345, -3))
}
func BenchmarkDecimal_NumDigits100(b *testing.B) {
s := make([]byte, 102)
for i := range s {
s[i] = byte('0' + i%10)
}
s[0] = '-'
s[100] = '.'
d, err := NewFromString(string(s))
if err != nil {
b.Log(d)
b.Error(err)
}
numDigits(b, 100, d)
}
func Benchmark_Cmp(b *testing.B) {
decimals := DecimalSlice([]Decimal{})
for i := 0; i < 1000000; i++ {
decimals = append(decimals, New(int64(i), 0))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sort.Sort(decimals)
}
}
func BenchmarkDecimal_Add_different_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
d1.Add(d2)
}
}
func BenchmarkDecimal_Sub_different_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
d1.Sub(d2)
}
}
func BenchmarkDecimal_Add_same_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500.123)
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
d1.Add(d2)
}
}
func BenchmarkDecimal_Sub_same_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500.123)
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
d1.Add(d2)
}
}
func BenchmarkDecimal_IsInteger(b *testing.B) {
d := RequireFromString("12.000")
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
d.IsInteger()
}
}
func BenchmarkDecimal_Pow(b *testing.B) {
d1 := RequireFromString("5.2")
d2 := RequireFromString("6.3")
for i := 0; i < b.N; i++ {
d1.Pow(d2)
}
}
func BenchmarkDecimal_PowWithPrecision(b *testing.B) {
d1 := RequireFromString("5.2")
d2 := RequireFromString("6.3")
for i := 0; i < b.N; i++ {
_, _ = d1.PowWithPrecision(d2, 8)
}
}
func BenchmarkDecimal_PowInt32(b *testing.B) {
d1 := RequireFromString("5.2")
d2 := int32(10)
for i := 0; i < b.N; i++ {
_, _ = d1.PowInt32(d2)
}
}
func BenchmarkDecimal_PowBigInt(b *testing.B) {
d1 := RequireFromString("5.2")
d2 := big.NewInt(10)
for i := 0; i < b.N; i++ {
_, _ = d1.PowBigInt(d2)
}
}
func BenchmarkDecimal_NewFromString(b *testing.B) {
count := 72
prices := make([]string, 0, count)
for i := 1; i <= count; i++ {
prices = append(prices, fmt.Sprintf("%d.%d", i*100, i))
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, p := range prices {
d, err := NewFromString(p)
if err != nil {
b.Log(d)
b.Error(err)
}
}
}
}
func BenchmarkDecimal_NewFromString_large_number(b *testing.B) {
count := 72
prices := make([]string, 0, count)
for i := 1; i <= count; i++ {
prices = append(prices, "9323372036854775807.9223372036854775807")
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, p := range prices {
d, err := NewFromString(p)
if err != nil {
b.Log(d)
b.Error(err)
}
}
}
}
func BenchmarkDecimal_ExpHullAbraham(b *testing.B) {
b.ResetTimer()
d := RequireFromString("30.412346346346")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = d.ExpHullAbrham(10)
}
}
func BenchmarkDecimal_ExpTaylor(b *testing.B) {
b.ResetTimer()
d := RequireFromString("30.412346346346")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = d.ExpTaylor(10)
}
}