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 http://www.opensource.org/licenses/mit-license.php.5#include <bench/bench.h>6#include <random.h>8#include <algorithm>9#include <cstdint>10#include <iterator>11#include <numeric>13namespace {15template<typename RNG>16void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept17{18bench.batch(1).unit("number").run([&] {19rng.rand64();20});21}23template<typename RNG>24void BenchRandom_rand32(benchmark::Bench& bench, RNG&& rng) noexcept25{26bench.batch(1).unit("number").run([&] {27rng.rand32();28});29}31template<typename RNG>32void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept33{34bench.batch(1).unit("number").run([&] {35rng.randbool();36});37}39template<typename RNG>40void BenchRandom_randbits(benchmark::Bench& bench, RNG&& rng) noexcept41{42bench.batch(64).unit("number").run([&] {43for (int i = 1; i <= 64; ++i) {44rng.randbits(i);45}46});47}49template<int RANGE, typename RNG>50void BenchRandom_randrange(benchmark::Bench& bench, RNG&& rng) noexcept51{52bench.batch(RANGE).unit("number").run([&] {53for (int i = 1; i <= RANGE; ++i) {54rng.randrange(i);55}56});57}59template<int RANGE, typename RNG>60void BenchRandom_stdshuffle(benchmark::Bench& bench, RNG&& rng) noexcept61{62uint64_t data[RANGE];63std::iota(std::begin(data), std::end(data), uint64_t(0));64bench.batch(RANGE).unit("number").run([&] {65std::shuffle(std::begin(data), std::end(data), rng);66});67}69void FastRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, FastRandomContext(true)); }70void FastRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, FastRandomContext(true)); }71void FastRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, FastRandomContext(true)); }72void FastRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, FastRandomContext(true)); }73void FastRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, FastRandomContext(true)); }74void FastRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, FastRandomContext(true)); }75void FastRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, FastRandomContext(true)); }76void FastRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, FastRandomContext(true)); }78void InsecureRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, InsecureRandomContext(251438)); }79void InsecureRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, InsecureRandomContext(251438)); }80void InsecureRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, InsecureRandomContext(251438)); }81void InsecureRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, InsecureRandomContext(251438)); }82void InsecureRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, InsecureRandomContext(251438)); }83void InsecureRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, InsecureRandomContext(251438)); }84void InsecureRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, InsecureRandomContext(251438)); }85void InsecureRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, InsecureRandomContext(251438)); }87} // namespace89BENCHMARK(FastRandom_rand64);90BENCHMARK(FastRandom_rand32);91BENCHMARK(FastRandom_randbool);92BENCHMARK(FastRandom_randbits);93BENCHMARK(FastRandom_randrange100);94BENCHMARK(FastRandom_randrange1000);95BENCHMARK(FastRandom_randrange1000000);96BENCHMARK(FastRandom_stdshuffle100);98BENCHMARK(InsecureRandom_rand64);99BENCHMARK(InsecureRandom_rand32);100BENCHMARK(InsecureRandom_randbool);101BENCHMARK(InsecureRandom_randbits);102BENCHMARK(InsecureRandom_randrange100);103BENCHMARK(InsecureRandom_randrange1000);104BENCHMARK(InsecureRandom_randrange1000000);105BENCHMARK(InsecureRandom_stdshuffle100);
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.