Bitcoin Core  29.1.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 
13 namespace {
14 
15 template<typename RNG>
16 void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept
17 {
18  bench.batch(1).unit("number").run([&] {
19  rng.rand64();
20  });
21 }
22 
23 template<typename RNG>
24 void BenchRandom_rand32(benchmark::Bench& bench, RNG&& rng) noexcept
25 {
26  bench.batch(1).unit("number").run([&] {
27  rng.rand32();
28  });
29 }
30 
31 template<typename RNG>
32 void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept
33 {
34  bench.batch(1).unit("number").run([&] {
35  rng.randbool();
36  });
37 }
38 
39 template<typename RNG>
40 void 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 
49 template<int RANGE, typename RNG>
50 void 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 
59 template<int RANGE, typename RNG>
60 void 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 
69 void FastRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, FastRandomContext(true)); }
70 void FastRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, FastRandomContext(true)); }
71 void FastRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, FastRandomContext(true)); }
72 void FastRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, FastRandomContext(true)); }
73 void FastRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, FastRandomContext(true)); }
74 void FastRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, FastRandomContext(true)); }
75 void FastRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, FastRandomContext(true)); }
76 void FastRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, FastRandomContext(true)); }
77 
78 void InsecureRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, InsecureRandomContext(251438)); }
79 void InsecureRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, InsecureRandomContext(251438)); }
80 void InsecureRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, InsecureRandomContext(251438)); }
81 void InsecureRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, InsecureRandomContext(251438)); }
82 void InsecureRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, InsecureRandomContext(251438)); }
83 void InsecureRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, InsecureRandomContext(251438)); }
84 void InsecureRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, InsecureRandomContext(251438)); }
85 void InsecureRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, InsecureRandomContext(251438)); }
86 
87 } // namespace
88 
89 BENCHMARK(FastRandom_rand64, benchmark::PriorityLevel::HIGH);
90 BENCHMARK(FastRandom_rand32, benchmark::PriorityLevel::HIGH);
91 BENCHMARK(FastRandom_randbool, benchmark::PriorityLevel::HIGH);
92 BENCHMARK(FastRandom_randbits, benchmark::PriorityLevel::HIGH);
93 BENCHMARK(FastRandom_randrange100, benchmark::PriorityLevel::HIGH);
94 BENCHMARK(FastRandom_randrange1000, benchmark::PriorityLevel::HIGH);
95 BENCHMARK(FastRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
96 BENCHMARK(FastRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
97 
98 BENCHMARK(InsecureRandom_rand64, benchmark::PriorityLevel::HIGH);
99 BENCHMARK(InsecureRandom_rand32, benchmark::PriorityLevel::HIGH);
100 BENCHMARK(InsecureRandom_randbool, benchmark::PriorityLevel::HIGH);
101 BENCHMARK(InsecureRandom_randbits, benchmark::PriorityLevel::HIGH);
102 BENCHMARK(InsecureRandom_randrange100, benchmark::PriorityLevel::HIGH);
103 BENCHMARK(InsecureRandom_randrange1000, benchmark::PriorityLevel::HIGH);
104 BENCHMARK(InsecureRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
105 BENCHMARK(InsecureRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
BENCHMARK(FastRandom_rand64, benchmark::PriorityLevel::HIGH)
Fast randomness source.
Definition: random.h:376
xoroshiro128++ PRNG.
Definition: random.h:415
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627