...

Source file src/gopkg.in/inf.v0/benchmark_test.go

Documentation: gopkg.in/inf.v0

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

View as plain text