Bitcoin Core 28.99.0
P2P Digital Currency
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 <algorithm>
9#include <cstdint>
10#include <iterator>
11#include <numeric>
12
13namespace {
14
15template<typename RNG>
16void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept
17{
18 bench.batch(1).unit("number").run([&] {
19 rng.rand64();
20 });
21}
22
23template<typename RNG>
24void BenchRandom_rand32(benchmark::Bench& bench, RNG&& rng) noexcept
25{
26 bench.batch(1).unit("number").run([&] {
27 rng.rand32();
28 });
29}
30
31template<typename RNG>
32void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept
33{
34 bench.batch(1).unit("number").run([&] {
35 rng.randbool();
36 });
37}
38
39template<typename RNG>
40void BenchRandom_randbits(benchmark::Bench& bench, RNG&& rng) noexcept
41{
42 bench.batch(64).unit("number").run([&] {
43 for (int i = 1; i <= 64; ++i) {
44 rng.randbits(i);
45 }
46 });
47}
48
49template<int RANGE, typename RNG>
50void BenchRandom_randrange(benchmark::Bench& bench, RNG&& rng) noexcept
51{
52 bench.batch(RANGE).unit("number").run([&] {
53 for (int i = 1; i <= RANGE; ++i) {
54 rng.randrange(i);
55 }
56 });
57}
58
59template<int RANGE, typename RNG>
60void BenchRandom_stdshuffle(benchmark::Bench& bench, RNG&& rng) noexcept
61{
62 uint64_t data[RANGE];
63 std::iota(std::begin(data), std::end(data), uint64_t(0));
64 bench.batch(RANGE).unit("number").run([&] {
65 std::shuffle(std::begin(data), std::end(data), rng);
66 });
67}
68
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)); }
77
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)); }
86
87} // namespace
88
93BENCHMARK(FastRandom_randrange100, benchmark::PriorityLevel::HIGH);
94BENCHMARK(FastRandom_randrange1000, benchmark::PriorityLevel::HIGH);
95BENCHMARK(FastRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
96BENCHMARK(FastRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
97
102BENCHMARK(InsecureRandom_randrange100, benchmark::PriorityLevel::HIGH);
103BENCHMARK(InsecureRandom_randrange1000, benchmark::PriorityLevel::HIGH);
104BENCHMARK(InsecureRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
105BENCHMARK(InsecureRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
BENCHMARK(FastRandom_rand64, benchmark::PriorityLevel::HIGH)
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:48