uint256: Add text formatting benchmarks.

The following is a comparison between stdlib big integers (old) and the
specialized type (new) averaging 10 runs each:

name             old time/op     new time/op     delta
---------------------------------------------------------------------------
Text/base_2      579ns ± 3%      496ns ± 2%      -14.37%  (p=0.000 n=10+10)
Text/base_8      266ns ± 1%      227ns ± 1%      -14.58%  (p=0.000 n=10+10)
Text/base_10     536ns ± 1%      458ns ± 2%      -14.58%  (p=0.000 n=10+10)
Text/base_16     205ns ± 2%      180ns ± 4%      -11.90%  (p=0.000 n=10+10)
Format/base_2    987ns ±15%      852ns ± 2%      -13.64%  (p=0.000 n=10+10)
Format/base_8    620ns ± 6%      544ns ± 3%      -12.31%  (p=0.000 n=10+10)
Format/base_10   888ns ± 1%      726ns ± 1%      -18.25%  (p=0.000 n=10+10)
Format/base_16   565ns ± 1%      449ns ± 1%      -20.41%  (p=0.000 n=10+10)

name             old allocs/op   new allocs/op   delta
--------------------------------------------------------------------------
Text/base_2      2.00 ± 0%       2.00 ± 0%         ~     (all equal)
Text/base_8      2.00 ± 0%       2.00 ± 0%         ~     (all equal)
Text/base_10     3.00 ± 0%       2.00 ± 0%      -33.33%  (p=0.000 n=10+10)
Text/base_16     2.00 ± 0%       2.00 ± 0%         ~     (all equal)
Format/base_2    5.00 ± 0%       3.00 ± 0%      -40.00%  (p=0.000 n=10+10)
Format/base_8    5.00 ± 0%       3.00 ± 0%      -40.00%  (p=0.000 n=10+10)
Format/base_10   6.00 ± 0%       3.00 ± 0%      -50.00%  (p=0.000 n=10+10)
Format/base_16   5.00 ± 0%       3.00 ± 0%      -40.00%  (p=0.000 n=10+10)

This is part of a series of commits to fully implement the uint256
package.
This commit is contained in:
Dave Collins 2021-11-06 16:07:44 -05:00
parent 4f7eaa879f
commit 4653cbc774
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2

View File

@ -18,6 +18,7 @@ var (
noElideBytes []byte
noElideInt int
noElideUint16 uint16
noElideString string
)
// randBenchVal houses values used throughout the benchmarks that are randomly
@ -1186,3 +1187,107 @@ func BenchmarkBigIntBitLen(b *testing.B) {
})
}
}
// BenchmarkUint256Text benchmarks converting an unsigned 256-bit integer to
// various output bases with the specialized type.
func BenchmarkUint256Text(b *testing.B) {
vals := randBenchVals
for _, base := range []OutputBase{2, 8, 10, 16} {
benchName := fmt.Sprintf("base %d", base)
b.Run(benchName, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideString = val.n1.Text(base)
}
}
})
}
}
// BenchmarkBigIntText benchmarks converting an unsigned 256-bit integer to
// various output bases with stdlib big integers.
func BenchmarkBigIntText(b *testing.B) {
vals := randBenchVals
for _, base := range []int{2, 8, 10, 16} {
benchName := fmt.Sprintf("base %d", base)
b.Run(benchName, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideString = val.bigN1.Text(base)
}
}
})
}
}
// BenchmarkUint256Format benchmarks formatting an unsigned 256-bit integer to
// various output bases via fmt.Sprintf with the specialized type.
func BenchmarkUint256Format(b *testing.B) {
vals := randBenchVals
for _, base := range []int{2, 8, 10, 16} {
benchName := fmt.Sprintf("base %d", base)
b.Run(benchName, func(b *testing.B) {
var fmtStr string
switch base {
case 2:
fmtStr = "%b"
case 8:
fmtStr = "%o"
case 10:
fmtStr = "%d"
case 16:
fmtStr = "%x"
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideString = fmt.Sprintf(fmtStr, val.n1)
}
}
})
}
}
// BenchmarkBigIntFormat benchmarks formatting an unsigned 256-bit integer to
// various output bases via fmt.Sprintf with stdlib big integers.
func BenchmarkBigIntFormat(b *testing.B) {
vals := randBenchVals
for _, base := range []int{2, 8, 10, 16} {
benchName := fmt.Sprintf("base %d", base)
b.Run(benchName, func(b *testing.B) {
var fmtStr string
switch base {
case 2:
fmtStr = "%b"
case 8:
fmtStr = "%o"
case 10:
fmtStr = "%d"
case 16:
fmtStr = "%x"
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideString = fmt.Sprintf(fmtStr, val.bigN1)
}
}
})
}
}