uint256: Add squaring 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
-------------------------------------------------------------------
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.
This commit is contained in:
Dave Collins 2021-11-06 16:07:16 -05:00
parent dc2a08f477
commit 8d27f653cd
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2

View File

@ -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)
}
}
}