This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_math_big_test.go
82 lines (76 loc) · 1.75 KB
/
bench_math_big_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
package gmp_test
import (
"math/big"
"testing"
)
// Make a n digit number
func nDigitNumberMathBig(digits int64) *big.Int {
x := big.NewInt(10)
n := big.NewInt(digits)
one := big.NewInt(1)
x.Exp(x, n, nil)
x.Sub(x, one)
return x
}
func benchmarkMathBigAddN(b *testing.B, digits int64) {
x := nDigitNumberMathBig(digits)
y := nDigitNumberMathBig(digits)
z := big.NewInt(0)
b.ResetTimer()
b.StartTimer()
for i := b.N - 1; i >= 0; i-- {
z.Add(x, y)
}
}
func BenchmarkMathBigAdd1(b *testing.B) {
benchmarkMathBigAddN(b, 10)
}
func BenchmarkMathBigAdd10(b *testing.B) {
benchmarkMathBigAddN(b, 10)
}
func BenchmarkMathBigAdd100(b *testing.B) {
benchmarkMathBigAddN(b, 100)
}
func BenchmarkMathBigAdd1000(b *testing.B) {
benchmarkMathBigAddN(b, 1000)
}
func BenchmarkMathBigAdd10000(b *testing.B) {
benchmarkMathBigAddN(b, 10000)
}
func BenchmarkMathBigAdd100000(b *testing.B) {
benchmarkMathBigAddN(b, 100000)
}
func BenchmarkMathBigAdd1000000(b *testing.B) {
benchmarkMathBigAddN(b, 1000000)
}
func benchmarkMathBigMulN(b *testing.B, digits int64) {
x := nDigitNumberMathBig(digits)
y := nDigitNumberMathBig(digits)
z := big.NewInt(0)
b.ResetTimer()
b.StartTimer()
for i := b.N - 1; i >= 0; i-- {
z.Mul(x, y)
}
}
func BenchmarkMathBigMul1(b *testing.B) {
benchmarkMathBigMulN(b, 10)
}
func BenchmarkMathBigMul10(b *testing.B) {
benchmarkMathBigMulN(b, 10)
}
func BenchmarkMathBigMul100(b *testing.B) {
benchmarkMathBigMulN(b, 100)
}
func BenchmarkMathBigMul1000(b *testing.B) {
benchmarkMathBigMulN(b, 1000)
}
func BenchmarkMathBigMul10000(b *testing.B) {
benchmarkMathBigMulN(b, 10000)
}
func BenchmarkMathBigMul100000(b *testing.B) {
benchmarkMathBigMulN(b, 100000)
}
func BenchmarkMathBigMul1000000(b *testing.B) {
benchmarkMathBigMulN(b, 1000000)
}