Bitcoin Core
31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
bench
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
);
90
BENCHMARK
(
FastRandom_rand32
);
91
BENCHMARK
(
FastRandom_randbool
);
92
BENCHMARK
(
FastRandom_randbits
);
93
BENCHMARK
(
FastRandom_randrange100
);
94
BENCHMARK
(
FastRandom_randrange1000
);
95
BENCHMARK
(
FastRandom_randrange1000000
);
96
BENCHMARK
(
FastRandom_stdshuffle100
);
97
98
BENCHMARK
(
InsecureRandom_rand64
);
99
BENCHMARK
(
InsecureRandom_rand32
);
100
BENCHMARK
(
InsecureRandom_randbool
);
101
BENCHMARK
(
InsecureRandom_randbits
);
102
BENCHMARK
(
InsecureRandom_randrange100
);
103
BENCHMARK
(
InsecureRandom_randrange1000
);
104
BENCHMARK
(
InsecureRandom_randrange1000000
);
105
BENCHMARK
(
InsecureRandom_stdshuffle100
);
bench.h
BENCHMARK
#define BENCHMARK(n)
Definition
bench.h:68
FastRandomContext
Fast randomness source.
Definition
random.h:386
InsecureRandomContext
xoroshiro128++ PRNG.
Definition
random.h:425
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:627
test_vectors_musig2_generate.data
data
Definition
test_vectors_musig2_generate.py:98
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:73
Generated on Thu Apr 16 2026 09:42:38 for Bitcoin Core by
1.10.0