You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

211 lines
4.1 KiB

  1. package inf
  2. import (
  3. "fmt"
  4. "math/big"
  5. "math/rand"
  6. "sync"
  7. "testing"
  8. )
  9. const maxcap = 1024 * 1024
  10. const bits = 256
  11. const maxscale = 32
  12. var once sync.Once
  13. var decInput [][2]Dec
  14. var intInput [][2]big.Int
  15. var initBench = func() {
  16. decInput = make([][2]Dec, maxcap)
  17. intInput = make([][2]big.Int, maxcap)
  18. max := new(big.Int).Lsh(big.NewInt(1), bits)
  19. r := rand.New(rand.NewSource(0))
  20. for i := 0; i < cap(decInput); i++ {
  21. decInput[i][0].SetUnscaledBig(new(big.Int).Rand(r, max)).
  22. SetScale(Scale(r.Int31n(int32(2*maxscale-1)) - int32(maxscale)))
  23. decInput[i][1].SetUnscaledBig(new(big.Int).Rand(r, max)).
  24. SetScale(Scale(r.Int31n(int32(2*maxscale-1)) - int32(maxscale)))
  25. }
  26. for i := 0; i < cap(intInput); i++ {
  27. intInput[i][0].Rand(r, max)
  28. intInput[i][1].Rand(r, max)
  29. }
  30. }
  31. func doBenchmarkDec1(b *testing.B, f func(z *Dec)) {
  32. once.Do(initBench)
  33. b.ResetTimer()
  34. b.StartTimer()
  35. for i := 0; i < b.N; i++ {
  36. f(&decInput[i%maxcap][0])
  37. }
  38. }
  39. func doBenchmarkDec2(b *testing.B, f func(x, y *Dec)) {
  40. once.Do(initBench)
  41. b.ResetTimer()
  42. b.StartTimer()
  43. for i := 0; i < b.N; i++ {
  44. f(&decInput[i%maxcap][0], &decInput[i%maxcap][1])
  45. }
  46. }
  47. func doBenchmarkInt1(b *testing.B, f func(z *big.Int)) {
  48. once.Do(initBench)
  49. b.ResetTimer()
  50. b.StartTimer()
  51. for i := 0; i < b.N; i++ {
  52. f(&intInput[i%maxcap][0])
  53. }
  54. }
  55. func doBenchmarkInt2(b *testing.B, f func(x, y *big.Int)) {
  56. once.Do(initBench)
  57. b.ResetTimer()
  58. b.StartTimer()
  59. for i := 0; i < b.N; i++ {
  60. f(&intInput[i%maxcap][0], &intInput[i%maxcap][1])
  61. }
  62. }
  63. func Benchmark_Dec_String(b *testing.B) {
  64. doBenchmarkDec1(b, func(x *Dec) {
  65. x.String()
  66. })
  67. }
  68. func Benchmark_Dec_StringScan(b *testing.B) {
  69. doBenchmarkDec1(b, func(x *Dec) {
  70. s := x.String()
  71. d := new(Dec)
  72. fmt.Sscan(s, d)
  73. })
  74. }
  75. func Benchmark_Dec_GobEncode(b *testing.B) {
  76. doBenchmarkDec1(b, func(x *Dec) {
  77. x.GobEncode()
  78. })
  79. }
  80. func Benchmark_Dec_GobEnDecode(b *testing.B) {
  81. doBenchmarkDec1(b, func(x *Dec) {
  82. g, _ := x.GobEncode()
  83. new(Dec).GobDecode(g)
  84. })
  85. }
  86. func Benchmark_Dec_Add(b *testing.B) {
  87. doBenchmarkDec2(b, func(x, y *Dec) {
  88. ys := y.Scale()
  89. y.SetScale(x.Scale())
  90. _ = new(Dec).Add(x, y)
  91. y.SetScale(ys)
  92. })
  93. }
  94. func Benchmark_Dec_AddMixed(b *testing.B) {
  95. doBenchmarkDec2(b, func(x, y *Dec) {
  96. _ = new(Dec).Add(x, y)
  97. })
  98. }
  99. func Benchmark_Dec_Sub(b *testing.B) {
  100. doBenchmarkDec2(b, func(x, y *Dec) {
  101. ys := y.Scale()
  102. y.SetScale(x.Scale())
  103. _ = new(Dec).Sub(x, y)
  104. y.SetScale(ys)
  105. })
  106. }
  107. func Benchmark_Dec_SubMixed(b *testing.B) {
  108. doBenchmarkDec2(b, func(x, y *Dec) {
  109. _ = new(Dec).Sub(x, y)
  110. })
  111. }
  112. func Benchmark_Dec_Mul(b *testing.B) {
  113. doBenchmarkDec2(b, func(x, y *Dec) {
  114. _ = new(Dec).Mul(x, y)
  115. })
  116. }
  117. func Benchmark_Dec_Mul_QuoExact(b *testing.B) {
  118. doBenchmarkDec2(b, func(x, y *Dec) {
  119. v := new(Dec).Mul(x, y)
  120. _ = new(Dec).QuoExact(v, y)
  121. })
  122. }
  123. func Benchmark_Dec_QuoRound_Fixed_Down(b *testing.B) {
  124. doBenchmarkDec2(b, func(x, y *Dec) {
  125. _ = new(Dec).QuoRound(x, y, 0, RoundDown)
  126. })
  127. }
  128. func Benchmark_Dec_QuoRound_Fixed_HalfUp(b *testing.B) {
  129. doBenchmarkDec2(b, func(x, y *Dec) {
  130. _ = new(Dec).QuoRound(x, y, 0, RoundHalfUp)
  131. })
  132. }
  133. func Benchmark_Int_String(b *testing.B) {
  134. doBenchmarkInt1(b, func(x *big.Int) {
  135. x.String()
  136. })
  137. }
  138. func Benchmark_Int_StringScan(b *testing.B) {
  139. doBenchmarkInt1(b, func(x *big.Int) {
  140. s := x.String()
  141. d := new(big.Int)
  142. fmt.Sscan(s, d)
  143. })
  144. }
  145. func Benchmark_Int_GobEncode(b *testing.B) {
  146. doBenchmarkInt1(b, func(x *big.Int) {
  147. x.GobEncode()
  148. })
  149. }
  150. func Benchmark_Int_GobEnDecode(b *testing.B) {
  151. doBenchmarkInt1(b, func(x *big.Int) {
  152. g, _ := x.GobEncode()
  153. new(big.Int).GobDecode(g)
  154. })
  155. }
  156. func Benchmark_Int_Add(b *testing.B) {
  157. doBenchmarkInt2(b, func(x, y *big.Int) {
  158. _ = new(big.Int).Add(x, y)
  159. })
  160. }
  161. func Benchmark_Int_Sub(b *testing.B) {
  162. doBenchmarkInt2(b, func(x, y *big.Int) {
  163. _ = new(big.Int).Sub(x, y)
  164. })
  165. }
  166. func Benchmark_Int_Mul(b *testing.B) {
  167. doBenchmarkInt2(b, func(x, y *big.Int) {
  168. _ = new(big.Int).Mul(x, y)
  169. })
  170. }
  171. func Benchmark_Int_Quo(b *testing.B) {
  172. doBenchmarkInt2(b, func(x, y *big.Int) {
  173. _ = new(big.Int).Quo(x, y)
  174. })
  175. }
  176. func Benchmark_Int_QuoRem(b *testing.B) {
  177. doBenchmarkInt2(b, func(x, y *big.Int) {
  178. _, _ = new(big.Int).QuoRem(x, y, new(big.Int))
  179. })
  180. }