Skip to content

Commit

Permalink
More benchs
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed May 14, 2024
1 parent 3d59321 commit 10fc6df
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions math/dec_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func BenchmarkCompareLegacyDecAndNewDecSum(b *testing.B) {
"1+2": {
summands: []string{"1", "2"},
},
"small numbers": {
summands: []string{"123", "0.2", "3.1415", "15"},
},
"medium numbers": {
summands: []string{"1234.567899", "9991345552.2340134"},
},
"big18": {
summands: []string{"123456789012345678", "123456789012345678", "123456789012345678", "123456789012345678", "123456789012345678", "123456789012345678"},
},

"growing numbers": {
summands: []string{"1", "100", "1000", "100000", "10000000", "10000000000", "10000000000000", "100000000000000000"},
},
Expand Down Expand Up @@ -109,6 +119,121 @@ func BenchmarkCompareLegacyDecAndNewDecSum(b *testing.B) {
}
}

func BenchmarkCompareLegacyDecAndNewDecSub(b *testing.B) {
specs := map[string]struct {
minuend string
subtrahends []string
}{
"100 - 1 - 2": {
minuend: "100",
subtrahends: []string{"1", "2"},
},
"small numbers": {
minuend: "152.4013",
subtrahends: []string{"123", "0.2", "3.1415", "15"},
},
"10000000 - big18 numbers": {
minuend: "10000000",
subtrahends: []string{"123456789012345678", "123456789012345678", "123456789012345678", "123456789012345678", "123456789012345678", "123456789012345678"},
},
"10000000 - growing numbers": {
minuend: "10000000",
subtrahends: []string{"1", "100", "1000", "100000", "10000000", "10000000000", "10000000000000", "100000000000000000"},
},
"10000000 shrinking decimals": {
minuend: "10000000",
subtrahends: []string{"0.1", "0.01", "0.001", "0.000001", "0.00000001", "0.00000000001", "0.00000000000001", "0.000000000000000001"},
},
}
for name, spec := range specs {
b.Run(name, func(b *testing.B) {
b.Run("LegacyDec", func(b *testing.B) {
summands := make([]LegacyDec, len(spec.subtrahends))
for i, s := range spec.subtrahends {
summands[i] = LegacyMustNewDecFromStr(s)
}
diff := LegacyMustNewDecFromStr(spec.minuend)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, s := range summands {
diff = diff.Sub(s)
}
}
})

b.Run("NewDec", func(b *testing.B) {
summands := make([]Dec, len(spec.subtrahends))
for i, s := range spec.subtrahends {
summands[i] = must(NewDecFromString(s))
}
diff := must(NewDecFromString(spec.minuend))
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, s := range summands {
diff, _ = diff.Sub(s)
}
}
})
})
}
}

func BenchmarkCompareLegacyDecAndNewDecMul(b *testing.B) {
specs := map[string]struct {
multiplier, multiplicant string
}{
"small/ small": {
multiplier: "100", multiplicant: "5",
},
"big18/ small": {
multiplier: "999999999999999999", multiplicant: "10",
},
"self18/ self18": {
multiplier: "999999999999999999", multiplicant: "999999999999999999",
},
"big18/ big18": {
multiplier: "888888888888888888", multiplicant: "444444444444444444",
},
"decimal18b/ decimal18c": {
multiplier: "8.88888888888888888", multiplicant: "4.1234567890123",
},
"small/ big18": {
multiplier: "100", multiplicant: "999999999999999999",
},
"big34/ big34": {
multiplier: "9999999999999999999999999999999999", multiplicant: "1999999999999999999999999999999999",
},
"negative big34": {
multiplier: "-9999999999999999999999999999999999", multiplicant: "999999999999999999999999999",
},
"decimal small": {
multiplier: "0.0000000001", multiplicant: "10",
},
"decimal small/decimal small ": {
multiplier: "0.0000000001", multiplicant: "0.0001",
},
}
for name, spec := range specs {
b.Run(name, func(b *testing.B) {
b.Run("LegacyDec", func(b *testing.B) {
dv, ds := LegacyMustNewDecFromStr(spec.multiplier), LegacyMustNewDecFromStr(spec.multiplicant)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = dv.Mul(ds)
}
})

b.Run("NewDec", func(b *testing.B) {
dv, ds := must(NewDecFromString(spec.multiplier)), must(NewDecFromString(spec.multiplicant))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = dv.Mul(ds)
}
})
})
}
}

func BenchmarkCompareLegacyDecAndNewDecQuoInteger(b *testing.B) {
legacyB1 := LegacyNewDec(100)
newB1 := NewDecFromInt64(100)
Expand Down

0 comments on commit 10fc6df

Please sign in to comment.