Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
slysterous committed Apr 18, 2021
1 parent 3d14641 commit fe53325
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 59 deletions.
175 changes: 118 additions & 57 deletions numeral_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,176 @@ import (
"github.com/slysterous/numeral"
)

func benchmarkNumeralIncrement(initialValue string , values []rune, b *testing.B){
num,err:=numeral.NewNumeral(values, initialValue)
func benchmarkNumeralIncrement(initialValue string, values []rune, b *testing.B) {
num, err := numeral.NewNumeral(values, initialValue)
if err != nil {
b.Fatal(err)
}
for n := 0; n < b.N; n++ {
_=num.Increment()
_ = num.Increment()
}
}

func benchmarkNumeralDecimal(num numeral.Numeral,b *testing.B) {
func benchmarkNumeralDecimal(num numeral.Numeral, b *testing.B) {
for n := 0; n < b.N; n++ {
_=num.Decimal()
_ = num.Decimal()
}
}

func benchmarkNumeralAdd(num numeral.Numeral,num2 numeral.Numeral,b *testing.B) {
func benchmarkNumeralAdd(num numeral.Numeral, num2 numeral.Numeral, b *testing.B) {
for n := 0; n < b.N; n++ {
_ = num.Add(num2)
}
}

func benchmarkNumeralSum(num numeral.Numeral, num2 numeral.Numeral, b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = numeral.Sum(testValues, num, num2)
}
}

func BenchmarkNumeralIncrementDecimalSmall(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
initValue:="0"
benchmarkNumeralIncrement(initValue,testValues,b)
initValue := "0"
benchmarkNumeralIncrement(initValue, testValues, b)
}

func BenchmarkNumeralIncrementDecimalLarge(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
initValue:="1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
benchmarkNumeralIncrement(initValue,testValues,b)
initValue := "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
benchmarkNumeralIncrement(initValue, testValues, b)
}

func BenchmarkNumeralIncrementBinarySmall(b *testing.B) {
testValues := []rune{'0', '1'}
initValue:="0"
benchmarkNumeralIncrement(initValue,testValues,b)
initValue := "0"
benchmarkNumeralIncrement(initValue, testValues, b)
}

func BenchmarkNumeralIncrementBinaryLarge(b *testing.B) {
testValues := []rune{'0', '1'}
initValue:="1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
benchmarkNumeralIncrement(initValue,testValues,b)
initValue := "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
benchmarkNumeralIncrement(initValue, testValues, b)
}

func BenchmarkNumeralIncrementHexSmall(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="0"
benchmarkNumeralIncrement(initValue,testValues,b)
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "0"
benchmarkNumeralIncrement(initValue, testValues, b)
}

func BenchmarkNumeralIncrementHexLarge(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
benchmarkNumeralIncrement(initValue,testValues,b)
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
benchmarkNumeralIncrement(initValue, testValues, b)
}

func BenchmarkNumeralDecimalFromHex0(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="0"
num,_:=numeral.NewNumeral(testValues,initValue)
benchmarkNumeralDecimal(*num,b)
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "0"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromHexfffff(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="ffffffffff"
num,_:=numeral.NewNumeral(testValues,initValue)
benchmarkNumeralDecimal(*num,b)
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "ffffffffff"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromHexffffffffff(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="ffffffffff"
num,_:=numeral.NewNumeral(testValues,initValue)
benchmarkNumeralDecimal(*num,b)
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "ffffffffff"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromHexffffffffffffffffffff(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="ffffffffffffffffffff"
num,_:=numeral.NewNumeral(testValues,initValue)
benchmarkNumeralDecimal(*num,b)
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "ffffffffffffffffffff"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromBinary0(b *testing.B) {
testValues := []rune{'0', '1'}
initValue := "0"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromBinary10000(b *testing.B) {
testValues := []rune{'0', '1'}
initValue := "10000"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromBinary1000000000(b *testing.B) {
testValues := []rune{'0', '1'}
initValue := "1000000000"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralDecimalFromBinary1000000000000000000(b *testing.B) {
testValues := []rune{'0', '1'}
initValue := "1000000000000000000"
num, _ := numeral.NewNumeral(testValues, initValue)
benchmarkNumeralDecimal(*num, b)
}

func BenchmarkNumeralAddBinaryOnHex(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
testValues2 := []rune{'0', '1'}
initValue := "999"
num, _ := numeral.NewNumeral(testValues, initValue)
num2, _ := numeral.NewFromDecimal(testValues2, 999000)
benchmarkNumeralAdd(*num, *num2, b)
}

func BenchmarkNumeralAddBinaryOnHex(b *testing.B){
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
testValues2:=[]rune{'0','1'}
initValue:="999"
num,_:=numeral.NewNumeral(testValues,initValue)
num2,_:=numeral.NewFromDecimal(testValues2,999000)
benchmarkNumeralAdd(*num,*num2,b)
func BenchmarkNumeralAddHexOnHex(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
testValues2 := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "999"
num, _ := numeral.NewNumeral(testValues, initValue)
num2, _ := numeral.NewFromDecimal(testValues2, 999000)
benchmarkNumeralAdd(*num, *num2, b)
}

func BenchmarkNumeralAddHexOnHex(b *testing.B){
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
testValues2:=[]rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'}
initValue:="999"
num,_:=numeral.NewNumeral(testValues,initValue)
num2,_:=numeral.NewFromDecimal(testValues2,999000)
benchmarkNumeralAdd(*num,*num2,b)
func BenchmarkNumeralAddDecOnDec(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
testValues2 := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
initValue := "999"
num, _ := numeral.NewNumeral(testValues, initValue)
num2, _ := numeral.NewFromDecimal(testValues2, 999000)
benchmarkNumeralAdd(*num, *num2, b)
}

func BenchmarkNumeralAddSumBinaryOnHex(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
testValues2 := []rune{'0', '1'}
initValue := "999"
num, _ := numeral.NewNumeral(testValues, initValue)
num2, _ := numeral.NewFromDecimal(testValues2, 999000)
benchmarkNumeralSum(*num, *num2, b)
}

func BenchmarkNumeralAddDecOnDec(b *testing.B){
func BenchmarkNumeralAddSumHexOnHex(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
testValues2 := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
initValue := "999"
num, _ := numeral.NewNumeral(testValues, initValue)
num2, _ := numeral.NewFromDecimal(testValues2, 999000)
benchmarkNumeralSum(*num, *num2, b)
}

func BenchmarkNumeralAddSumDecOnDec(b *testing.B) {
testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
testValues2:=[]rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
initValue:="999"
num,_:=numeral.NewNumeral(testValues,initValue)
num2,_:=numeral.NewFromDecimal(testValues2,999000)
benchmarkNumeralAdd(*num,*num2,b)
}
testValues2 := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
initValue := "999"
num, _ := numeral.NewNumeral(testValues, initValue)
num2, _ := numeral.NewFromDecimal(testValues2, 999000)
benchmarkNumeralSum(*num, *num2, b)
}
4 changes: 2 additions & 2 deletions numeral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestDecrementOnZeroThrowsErr(t *testing.T) {
if err == nil {
t.Errorf("expected error to be thrown on Decrement")
}
if number.String()!="0"{
t.Errorf("expected: 0, got: %s ",number.String())
if number.String() != "0" {
t.Errorf("expected: 0, got: %s ", number.String())
}
}

0 comments on commit fe53325

Please sign in to comment.