Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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) {
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>
45{
46 const auto pairs{MakePairs(difference)};
47 bench.batch(pairs.size()).unit("comparison").run([&] {
48 for (const auto& [lhs, rhs] : pairs) {
50 }
51 });
52}
53
55{
56 Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });
57}
58
60{
61 Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });
62}
63
65{
66 Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });
67}
68
70{
71 Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });
72}
73
75{
76 Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });
77}
78
80{
81 Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });
82}
83
84} // namespace
85
#define BENCHMARK(n)
Definition bench.h:69
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
static constexpr unsigned int size()
Definition uint256.h:107
constexpr unsigned char * begin()
Definition uint256.h:101
256-bit opaque blob.
Definition uint256.h:196
void doNotOptimizeAway(Arg &&arg)
Makes sure none of the given arguments are optimized away by the compiler.
Definition nanobench.h:1353
FastRandomContext rng
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82