From 8d27f653cdfa9ac289355ea3e48494df9cfecbe9 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 6 Nov 2021 16:07:16 -0500 Subject: [PATCH] uint256: Add squaring 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 ------------------------------------------------------------------- Square 418ns ± 0% 7ns ± 2% -98.39% (p=0.000 n=10+10) name old allocs/op new allocs/op delta -------------------------------------------------------------------- Square 1.00 ± 0% 0.00 -100.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 | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/internal/staging/primitives/uint256/uint256_bench_test.go b/internal/staging/primitives/uint256/uint256_bench_test.go index 8133e351..388f4253 100644 --- a/internal/staging/primitives/uint256/uint256_bench_test.go +++ b/internal/staging/primitives/uint256/uint256_bench_test.go @@ -616,3 +616,37 @@ func BenchmarkBigIntMulUint64(b *testing.B) { } } } + +// BenchmarkUint256Square benchmarks computing the quotient of unsigned 256-bit +// integers with the specialized type. +func BenchmarkUint256Square(b *testing.B) { + n := new(Uint256) + vals := randBenchVals + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i += len(vals) { + for j := 0; j < len(vals); j++ { + val := &vals[j] + n.SquareVal(val.n1) + } + } +} + +// BenchmarkBigIntSquare benchmarks computing the quotient of unsigned 256-bit +// integers with stdlib big integers. +func BenchmarkBigIntSquare(b *testing.B) { + n := new(big.Int) + two256 := new(big.Int).Lsh(big.NewInt(1), 256) + vals := randBenchVals + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i += len(vals) { + for j := 0; j < len(vals); j++ { + val := &vals[j] + n.Mul(val.bigN1, val.bigN1) + n.Mod(n, two256) + } + } +}