Skip to content

Commit

Permalink
primitives: Add check proof of work benchmark.
Browse files Browse the repository at this point in the history
The following is a comparison between the existing implementation in the
blockchain/standalone module with big integers (old) and the new
implementation (new) averaging 10 runs each:

name               old time/op     new time/op     delta
-----------------------------------------------------------------------------
CheckProofOfWork   314ns ± 1%      43ns ± 0%       -86.42%  (p=0.000 n=10+10)

name               old allocs/op   new allocs/op   delta
------------------------------------------------------------------------------
CheckProofOfWork   5.00 ± 0%       0.00            -100.00%  (p=0.000 n=10+10)
  • Loading branch information
davecgh committed Dec 4, 2021
1 parent 03bacc6 commit c7be7b6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/staging/primitives/pow_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,22 @@ func BenchmarkHashToUint256(b *testing.B) {
HashToUint256(hash)
}
}

// BenchmarkCheckProofOfWork benchmarks ensuring a given block hash satisfies
// the proof of work requirements for given difficulty bits.
func BenchmarkCheckProofOfWork(b *testing.B) {
// Data from block 100k on the main network.
h := "00000000000004289d9a7b0f7a332fb60a1c221faae89a107ce3ab93eead2f93"
blockHash, err := chainhash.NewHashFromStr(h)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
const diffBits = 0x1a1194b4
powLimit := hexToUint256(mockMainNetPowLimit())

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = CheckProofOfWork(blockHash, diffBits, powLimit)
}
}

0 comments on commit c7be7b6

Please sign in to comment.