Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://opensource.org/license/mit.5#include <bench/bench.h>6#include <random.h>7#include <uint256.h>9#include <compare>10#include <cstddef>11#include <utility>12#include <vector>14namespace {16enum class Difference {17NONE,18FIRST_BYTE,19LAST_BYTE,20};22constexpr size_t NUM_PAIRS{4'096};24std::vector<std::pair<uint256, uint256>> MakePairs(Difference difference)25{26FastRandomContext rng{/*fDeterministic=*/true};27std::vector<std::pair<uint256, uint256>> pairs;28pairs.reserve(NUM_PAIRS);30for (size_t i{0}; i < NUM_PAIRS; ++i) {31uint256 lhs{rng.rand256()};32uint256 rhs{lhs};33if (difference != Difference::NONE) {34const size_t position{difference == Difference::FIRST_BYTE ? 0 : uint256::size() - 1};35lhs.begin()[position] = i % 2 == 0 ? 0 : 255;36rhs.begin()[position] = i % 2 == 0 ? 255 : 0;37}38pairs.emplace_back(lhs, rhs);39}40return pairs;41}43template <typename Comparator>44void Comparison(benchmark::Bench& bench, Difference difference, Comparator comparator)45{46const auto pairs{MakePairs(difference)};47bench.batch(pairs.size()).unit("comparison").run([&] {48for (const auto& [lhs, rhs] : pairs) {49ankerl::nanobench::doNotOptimizeAway(comparator(lhs, rhs));50}51});52}54void Uint256EqualIdentical(benchmark::Bench& bench)55{56Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });57}59void Uint256EqualFirstByteDifferent(benchmark::Bench& bench)60{61Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });62}64void Uint256EqualLastByteDifferent(benchmark::Bench& bench)65{66Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; });67}69void Uint256LessIdentical(benchmark::Bench& bench)70{71Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });72}74void Uint256LessFirstByteDifferent(benchmark::Bench& bench)75{76Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });77}79void Uint256LessLastByteDifferent(benchmark::Bench& bench)80{81Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; });82}84} // namespace86BENCHMARK(Uint256EqualIdentical);87BENCHMARK(Uint256EqualFirstByteDifferent);88BENCHMARK(Uint256EqualLastByteDifferent);89BENCHMARK(Uint256LessIdentical);90BENCHMARK(Uint256LessFirstByteDifferent);91BENCHMARK(Uint256LessLastByteDifferent);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.