github.com/dubbogo/gost@v1.14.0/math/big/decimal_benchmark_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxbig
    19  
    20  import (
    21  	"math"
    22  	"math/rand"
    23  	"strconv"
    24  	"testing"
    25  )
    26  
    27  const (
    28  	numTestDec = 1000
    29  )
    30  
    31  var (
    32  	testDec []Decimal
    33  	flag    = false
    34  )
    35  
    36  func genTestDecimals() {
    37  	if flag {
    38  		return
    39  	}
    40  	for i := 0; i < numTestDec; i++ {
    41  		f := rand.Float64()
    42  		digits := rand.Int()%12 + 1
    43  		offset := rand.Int()%digits + 1
    44  		f = math.Round(f*math.Pow10(digits)) / math.Pow10(digits-offset)
    45  		val, _ := NewDecFromFloat(f)
    46  		testDec = append(testDec, *val)
    47  	}
    48  	flag = true
    49  }
    50  
    51  func BenchmarkRound(b *testing.B) {
    52  	b.StopTimer()
    53  	var roundTo Decimal
    54  	tests := []struct {
    55  		input    string
    56  		scale    int
    57  		inputDec Decimal
    58  	}{
    59  		{input: "123456789.987654321", scale: 1},
    60  		{input: "15.1", scale: 0},
    61  		{input: "15.5", scale: 0},
    62  		{input: "15.9", scale: 0},
    63  		{input: "-15.1", scale: 0},
    64  		{input: "-15.5", scale: 0},
    65  		{input: "-15.9", scale: 0},
    66  		{input: "15.1", scale: 1},
    67  		{input: "-15.1", scale: 1},
    68  		{input: "15.17", scale: 1},
    69  		{input: "15.4", scale: -1},
    70  		{input: "-15.4", scale: -1},
    71  		{input: "5.4", scale: -1},
    72  		{input: ".999", scale: 0},
    73  		{input: "999999999", scale: -9},
    74  	}
    75  
    76  	for i := 0; i < len(tests); i++ {
    77  		err := tests[i].inputDec.FromString(tests[i].input)
    78  		if err != nil {
    79  			b.Fatal(err)
    80  		}
    81  	}
    82  
    83  	b.StartTimer()
    84  	for n := 0; n < b.N; n++ {
    85  		for i := 0; i < len(tests); i++ {
    86  			err := tests[i].inputDec.Round(&roundTo, tests[i].scale, ModeHalfEven)
    87  			if err != nil {
    88  				b.Fatal(err)
    89  			}
    90  		}
    91  		for i := 0; i < len(tests); i++ {
    92  			err := tests[i].inputDec.Round(&roundTo, tests[i].scale, ModeTruncate)
    93  			if err != nil {
    94  				b.Fatal(err)
    95  			}
    96  		}
    97  		for i := 0; i < len(tests); i++ {
    98  			err := tests[i].inputDec.Round(&roundTo, tests[i].scale, modeCeiling)
    99  			if err != nil {
   100  				b.Fatal(err)
   101  			}
   102  		}
   103  	}
   104  }
   105  
   106  func BenchmarkToFloat64New(b *testing.B) {
   107  	genTestDecimals()
   108  	b.ResetTimer()
   109  	for i := 0; i < b.N; i++ {
   110  		for j := 0; j < numTestDec; j++ {
   111  			f, _ := testDec[j].ToFloat64()
   112  			_ = f
   113  		}
   114  	}
   115  }
   116  
   117  func BenchmarkToFloat64Old(b *testing.B) {
   118  	genTestDecimals()
   119  	b.ResetTimer()
   120  	for i := 0; i < b.N; i++ {
   121  		for j := 0; j < numTestDec; j++ {
   122  			f, _ := strconv.ParseFloat(testDec[j].String(), 64)
   123  			_ = f
   124  		}
   125  	}
   126  }
   127  
   128  func benchmarkMyDecimalToBinOrHashCases() []string {
   129  	return []string{
   130  		"1.000000000000", "3", "12.000000000", "120",
   131  		"120000", "100000000000.00000", "0.000000001200000000",
   132  		"98765.4321", "-123.456000000000000000",
   133  		"0", "0000000000", "0.00000000000",
   134  	}
   135  }
   136  
   137  func BenchmarkMyDecimalToBin(b *testing.B) {
   138  	cases := benchmarkMyDecimalToBinOrHashCases()
   139  	decs := make([]*Decimal, 0, len(cases))
   140  	for _, ca := range cases {
   141  		var dec Decimal
   142  		if err := dec.FromString(ca); err != nil {
   143  			b.Fatal(err)
   144  		}
   145  		decs = append(decs, &dec)
   146  	}
   147  
   148  	b.ResetTimer()
   149  	for i := 0; i < b.N; i++ {
   150  		for _, dec := range decs {
   151  			prec, frac := dec.PrecisionAndFrac()
   152  			_, err := dec.ToBin(prec, frac)
   153  			if err != nil {
   154  				b.Fatal(err)
   155  			}
   156  		}
   157  	}
   158  }
   159  
   160  func BenchmarkMyDecimalToHashKey(b *testing.B) {
   161  	cases := benchmarkMyDecimalToBinOrHashCases()
   162  	decs := make([]*Decimal, 0, len(cases))
   163  	for _, ca := range cases {
   164  		var dec Decimal
   165  		if err := dec.FromString(ca); err != nil {
   166  			b.Fatal(err)
   167  		}
   168  		decs = append(decs, &dec)
   169  	}
   170  
   171  	b.ResetTimer()
   172  	for i := 0; i < b.N; i++ {
   173  		for _, dec := range decs {
   174  			_, err := dec.ToHashKey()
   175  			if err != nil {
   176  				b.Fatal(err)
   177  			}
   178  		}
   179  	}
   180  }