From 4653cbc77490e0cdebf3fc414e00217ab96f876a Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 6 Nov 2021 16:07:44 -0500 Subject: [PATCH] uint256: Add text formatting benchmarks. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../primitives/uint256/uint256_bench_test.go | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/internal/staging/primitives/uint256/uint256_bench_test.go b/internal/staging/primitives/uint256/uint256_bench_test.go index 53628517..36d49a62 100644 --- a/internal/staging/primitives/uint256/uint256_bench_test.go +++ b/internal/staging/primitives/uint256/uint256_bench_test.go @@ -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) + } + } + }) + } +}