Bitcoin Core 31.99.0
P2P Digital Currency
uint256_blob.cpp
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit.
4
5#include <bench/bench.h>
6#include <random.h>
7#include <uint256.h>
8
9#include <compare>
10#include <cstddef>
11#include <utility>
12#include <vector>
13
14namespace {
15
16enum class Difference {
17 NONE,
18 FIRST_BYTE,
19 LAST_BYTE,
20};
21
22constexpr size_t NUM_PAIRS{4'096};
23
24std::vector<std::pair<uint256, uint256>> MakePairs(Difference difference)
25{
26 FastRandomContext rng{/*fDeterministic=*/true};
27 std::vector<std::pair<uint256, uint256>> pairs;
28 pairs.reserve(NUM_PAIRS);
29
30 for (size_t i{0}; i < NUM_PAIRS; ++i) {
31 uint256 lhs{rng.rand256()};
32 uint256 rhs{lhs};
33 if (difference != Difference::NONE) {
34 const size_t position{difference == Difference::FIRST_BYTE ? 0 : uint256::size() - 1};
35 lhs.begin()[position] = i % 2 == 0 ? 0 : 255;
36 rhs.begin()[position] = i % 2 == 0 ? 255 : 0;
37 }
38 pairs.emplace_back(lhs, rhs);
39 }
40 return pairs;
41}
42
43template <typename Comparator>
44void Comparison(benchmark::Bench& bench, Difference difference, Comparator comparator)
45{
46 const auto pairs{MakePairs(difference)};
47 bench.batch(pairs.size()).unit("comparison").run([&] {
48 for (const auto& [lhs, rhs] : pairs) {
49 ankerl::nanobench::doNotOptimizeAway(comparator(lhs, rhs));
50 }
51 });
52}
53
54void Uint256EqualIdentical(benchmark::Bench& bench)
55{
56 Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });
57}
58
59void Uint256EqualFirstByteDifferent(benchmark::Bench& bench)
60{
61 Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });
62}
63
64void Uint256EqualLastByteDifferent(benchmark::Bench& bench)
65{
66 Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });
67}
68
69void Uint256LessIdentical(benchmark::Bench& bench)
70{
71 Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });
72}
73
74void Uint256LessFirstByteDifferent(benchmark::Bench& bench)
75{
76 Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });
77}
78
79void Uint256LessLastByteDifferent(benchmark::Bench& bench)
80{
81 Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });
82}
83
84} // namespace
85
86BENCHMARK(Uint256EqualIdentical);
87BENCHMARK(Uint256EqualFirstByteDifferent);
88BENCHMARK(Uint256EqualLastByteDifferent);
89BENCHMARK(Uint256LessIdentical);
90BENCHMARK(Uint256LessFirstByteDifferent);
91BENCHMARK(Uint256LessLastByteDifferent);
Fast randomness source.
Definition: random.h:386
uint256 rand256() noexcept
generate a random uint256.
Definition: random.h:317
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:649
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1308
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1332
Bench & unit(char const *unit)
Sets the operation unit.
static constexpr unsigned int size()
Definition: uint256.h:107
256-bit opaque blob.
Definition: uint256.h:196
@ NONE
Definition: categories.h:15
void doNotOptimizeAway(Arg &&arg)
Makes sure none of the given arguments are optimized away by the compiler.
Definition: nanobench.h:1353
FastRandomContext rng
Definition: dbwrapper.cpp:413
BENCHMARK(Uint256EqualIdentical)