Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
random.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 http://www.opensource.org/licenses/mit-license.php.
4
5#include <bench/bench.h>
6#include <random.h>
7
8#include <cstdint>
9#include <numeric>
10
11namespace {
12
13template<typename RNG>
14void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept
15{
16 bench.batch(1).unit("number").run([&] {
17 rng.rand64();
18 });
19}
20
21template<typename RNG>
22void BenchRandom_rand32(benchmark::Bench& bench, RNG&& rng) noexcept
23{
24 bench.batch(1).unit("number").run([&] {
25 rng.rand32();
26 });
27}
28
29template<typename RNG>
30void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept
31{
32 bench.batch(1).unit("number").run([&] {
33 rng.randbool();
34 });
35}
36
37template<typename RNG>
38void BenchRandom_randbits(benchmark::Bench& bench, RNG&& rng) noexcept
39{
40 bench.batch(64).unit("number").run([&] {
41 for (int i = 1; i <= 64; ++i) {
42 rng.randbits(i);
43 }
44 });
45}
46
47template<int RANGE, typename RNG>
48void BenchRandom_randrange(benchmark::Bench& bench, RNG&& rng) noexcept
49{
50 bench.batch(RANGE).unit("number").run([&] {
51 for (int i = 1; i <= RANGE; ++i) {
52 rng.randrange(i);
53 }
54 });
55}
56
57template<int RANGE, typename RNG>
58void BenchRandom_stdshuffle(benchmark::Bench& bench, RNG&& rng) noexcept
59{
60 uint64_t data[RANGE];
61 std::iota(std::begin(data), std::end(data), uint64_t(0));
62 bench.batch(RANGE).unit("number").run([&] {
63 std::shuffle(std::begin(data), std::end(data), rng);
64 });
65}
66
67void FastRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, FastRandomContext(true)); }
68void FastRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, FastRandomContext(true)); }
69void FastRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, FastRandomContext(true)); }
70void FastRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, FastRandomContext(true)); }
71void FastRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, FastRandomContext(true)); }
72void FastRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, FastRandomContext(true)); }
73void FastRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, FastRandomContext(true)); }
74void FastRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, FastRandomContext(true)); }
75
76void InsecureRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, InsecureRandomContext(251438)); }
77void InsecureRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, InsecureRandomContext(251438)); }
78void InsecureRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, InsecureRandomContext(251438)); }
79void InsecureRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, InsecureRandomContext(251438)); }
80void InsecureRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, InsecureRandomContext(251438)); }
81void InsecureRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, InsecureRandomContext(251438)); }
82void InsecureRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, InsecureRandomContext(251438)); }
83void InsecureRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, InsecureRandomContext(251438)); }
84
85} // namespace
86
91BENCHMARK(FastRandom_randrange100, benchmark::PriorityLevel::HIGH);
92BENCHMARK(FastRandom_randrange1000, benchmark::PriorityLevel::HIGH);
93BENCHMARK(FastRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
94BENCHMARK(FastRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
95
98BENCHMARK(InsecureRandom_randbool, benchmark::PriorityLevel::HIGH);
99BENCHMARK(InsecureRandom_randbits, benchmark::PriorityLevel::HIGH);
100BENCHMARK(InsecureRandom_randrange100, benchmark::PriorityLevel::HIGH);
101BENCHMARK(InsecureRandom_randrange1000, benchmark::PriorityLevel::HIGH);
102BENCHMARK(InsecureRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
103BENCHMARK(InsecureRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
#define BENCHMARK(n, priority_level)
Definition bench.h:79
Fast randomness source.
Definition random.h:377
xoroshiro128++ PRNG.
Definition random.h:416
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:627
@ HIGH
Definition bench.h:47