github.com/richardwilkes/toolbox@v1.121.0/xmath/num/benchmarks_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package num_test 11 12 import ( 13 "math/big" 14 "testing" 15 16 "github.com/richardwilkes/toolbox/xmath/num" 17 ) 18 19 const ( 20 leftStr = "2307687492367321180488" 21 rightStr = "8022819849149681007238941328161" 22 shortRightStr = "8491817238132811" 23 ) 24 25 var ( 26 benchInt128 num.Int128 27 benchUint128 num.Uint128 28 benchBigInt *big.Int 29 ) 30 31 func BenchmarkInt128Add(b *testing.B) { 32 left := num.Int128FromStringNoCheck(leftStr) 33 right := num.Int128FromStringNoCheck(rightStr) 34 var dest num.Int128 35 for i := 0; i < b.N; i++ { 36 dest = left.Add(right) 37 } 38 benchInt128 = dest 39 } 40 41 func BenchmarkUint128Add(b *testing.B) { 42 left := num.Uint128FromStringNoCheck(leftStr) 43 right := num.Uint128FromStringNoCheck(rightStr) 44 var dest num.Uint128 45 for i := 0; i < b.N; i++ { 46 dest = left.Add(right) 47 } 48 benchUint128 = dest 49 } 50 51 func BenchmarkBigIntAdd(b *testing.B) { 52 left, _ := new(big.Int).SetString(leftStr, 0) 53 right, _ := new(big.Int).SetString(rightStr, 0) 54 dest := new(big.Int) 55 for i := 0; i < b.N; i++ { 56 dest.Add(left, right) 57 } 58 benchBigInt = dest 59 } 60 61 func BenchmarkInt128Sub(b *testing.B) { 62 left := num.Int128FromStringNoCheck(leftStr) 63 right := num.Int128FromStringNoCheck(rightStr) 64 var dest num.Int128 65 for i := 0; i < b.N; i++ { 66 dest = left.Sub(right) 67 } 68 benchInt128 = dest 69 } 70 71 func BenchmarkUint128Sub(b *testing.B) { 72 left := num.Uint128FromStringNoCheck(leftStr) 73 right := num.Uint128FromStringNoCheck(rightStr) 74 var dest num.Uint128 75 for i := 0; i < b.N; i++ { 76 dest = left.Sub(right) 77 } 78 benchUint128 = dest 79 } 80 81 func BenchmarkBigIntSub(b *testing.B) { 82 left, _ := new(big.Int).SetString(leftStr, 0) 83 right, _ := new(big.Int).SetString(rightStr, 0) 84 dest := new(big.Int) 85 for i := 0; i < b.N; i++ { 86 dest.Sub(left, right) 87 } 88 benchBigInt = dest 89 } 90 91 func BenchmarkInt128Mul(b *testing.B) { 92 left := num.Int128FromStringNoCheck(leftStr) 93 right := num.Int128FromStringNoCheck(shortRightStr) 94 var dest num.Int128 95 for i := 0; i < b.N; i++ { 96 dest = left.Mul(right) 97 } 98 benchInt128 = dest 99 } 100 101 func BenchmarkUint128Mul(b *testing.B) { 102 left := num.Uint128FromStringNoCheck(leftStr) 103 right := num.Uint128FromStringNoCheck(shortRightStr) 104 var dest num.Uint128 105 for i := 0; i < b.N; i++ { 106 dest = left.Mul(right) 107 } 108 benchUint128 = dest 109 } 110 111 func BenchmarkBigIntMul(b *testing.B) { 112 left, _ := new(big.Int).SetString(leftStr, 0) 113 right, _ := new(big.Int).SetString(shortRightStr, 0) 114 dest := new(big.Int) 115 for i := 0; i < b.N; i++ { 116 dest.Mul(left, right) 117 } 118 benchBigInt = dest 119 } 120 121 func BenchmarkInt128Div(b *testing.B) { 122 left := num.Int128FromStringNoCheck(leftStr) 123 right := num.Int128FromStringNoCheck(shortRightStr) 124 var dest num.Int128 125 for i := 0; i < b.N; i++ { 126 dest = left.Div(right) 127 } 128 benchInt128 = dest 129 } 130 131 func BenchmarkUint128Div(b *testing.B) { 132 left := num.Uint128FromStringNoCheck(leftStr) 133 right := num.Uint128FromStringNoCheck(shortRightStr) 134 var dest num.Uint128 135 for i := 0; i < b.N; i++ { 136 dest = left.Div(right) 137 } 138 benchUint128 = dest 139 } 140 141 func BenchmarkBigIntDiv(b *testing.B) { 142 left, _ := new(big.Int).SetString(leftStr, 0) 143 right, _ := new(big.Int).SetString(shortRightStr, 0) 144 dest := new(big.Int) 145 for i := 0; i < b.N; i++ { 146 dest.Div(left, right) 147 } 148 benchBigInt = dest 149 }