Skip to content

Commit da28a26

Browse files
committed
bench random: benchmark more functions, and add InsecureRandomContext
Also rename the benchmark names to match the operation names
1 parent 0a9bbc6 commit da28a26

File tree

1 file changed

+100
-8
lines changed

1 file changed

+100
-8
lines changed

src/bench/random.cpp

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,113 @@
55
#include <bench/bench.h>
66
#include <random.h>
77

8-
static void FastRandom_32bit(benchmark::Bench& bench)
8+
#include <cstdint>
9+
#include <numeric>
10+
11+
namespace {
12+
13+
template<typename RNG>
14+
void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept
915
{
10-
FastRandomContext rng(true);
11-
bench.run([&] {
16+
bench.batch(1).unit("number").run([&] {
17+
rng.rand64();
18+
});
19+
}
20+
21+
template<typename RNG>
22+
void BenchRandom_rand32(benchmark::Bench& bench, RNG&& rng) noexcept
23+
{
24+
bench.batch(1).unit("number").run([&] {
1225
rng.rand32();
1326
});
1427
}
1528

16-
static void FastRandom_1bit(benchmark::Bench& bench)
29+
template<typename RNG>
30+
void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept
1731
{
18-
FastRandomContext rng(true);
19-
bench.run([&] {
32+
bench.batch(1).unit("number").run([&] {
2033
rng.randbool();
2134
});
2235
}
2336

24-
BENCHMARK(FastRandom_32bit, benchmark::PriorityLevel::HIGH);
25-
BENCHMARK(FastRandom_1bit, benchmark::PriorityLevel::HIGH);
37+
template<typename RNG>
38+
void 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+
47+
template<int RANGE, typename RNG>
48+
void 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+
57+
template<int RANGE, typename RNG>
58+
void BenchRandom_Shuffle(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+
Shuffle(std::begin(data), std::end(data), rng);
64+
});
65+
}
66+
67+
template<int RANGE, typename RNG>
68+
void BenchRandom_stdshuffle(benchmark::Bench& bench, RNG&& rng) noexcept
69+
{
70+
uint64_t data[RANGE];
71+
std::iota(std::begin(data), std::end(data), uint64_t(0));
72+
bench.batch(RANGE).unit("number").run([&] {
73+
std::shuffle(std::begin(data), std::end(data), rng);
74+
});
75+
}
76+
77+
void FastRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, FastRandomContext(true)); }
78+
void FastRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, FastRandomContext(true)); }
79+
void FastRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, FastRandomContext(true)); }
80+
void FastRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, FastRandomContext(true)); }
81+
void FastRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, FastRandomContext(true)); }
82+
void FastRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, FastRandomContext(true)); }
83+
void FastRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, FastRandomContext(true)); }
84+
void FastRandom_Shuffle100(benchmark::Bench& bench) { BenchRandom_Shuffle<100>(bench, FastRandomContext(true)); }
85+
void FastRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, FastRandomContext(true)); }
86+
87+
void InsecureRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, InsecureRandomContext(251438)); }
88+
void InsecureRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, InsecureRandomContext(251438)); }
89+
void InsecureRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, InsecureRandomContext(251438)); }
90+
void InsecureRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, InsecureRandomContext(251438)); }
91+
void InsecureRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, InsecureRandomContext(251438)); }
92+
void InsecureRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, InsecureRandomContext(251438)); }
93+
void InsecureRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, InsecureRandomContext(251438)); }
94+
void InsecureRandom_Shuffle100(benchmark::Bench& bench) { BenchRandom_Shuffle<100>(bench, InsecureRandomContext(251438)); }
95+
void InsecureRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, InsecureRandomContext(251438)); }
96+
97+
} // namespace
98+
99+
BENCHMARK(FastRandom_rand64, benchmark::PriorityLevel::HIGH);
100+
BENCHMARK(FastRandom_rand32, benchmark::PriorityLevel::HIGH);
101+
BENCHMARK(FastRandom_randbool, benchmark::PriorityLevel::HIGH);
102+
BENCHMARK(FastRandom_randbits, benchmark::PriorityLevel::HIGH);
103+
BENCHMARK(FastRandom_randrange100, benchmark::PriorityLevel::HIGH);
104+
BENCHMARK(FastRandom_randrange1000, benchmark::PriorityLevel::HIGH);
105+
BENCHMARK(FastRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
106+
BENCHMARK(FastRandom_Shuffle100, benchmark::PriorityLevel::HIGH);
107+
BENCHMARK(FastRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);
108+
109+
BENCHMARK(InsecureRandom_rand64, benchmark::PriorityLevel::HIGH);
110+
BENCHMARK(InsecureRandom_rand32, benchmark::PriorityLevel::HIGH);
111+
BENCHMARK(InsecureRandom_randbool, benchmark::PriorityLevel::HIGH);
112+
BENCHMARK(InsecureRandom_randbits, benchmark::PriorityLevel::HIGH);
113+
BENCHMARK(InsecureRandom_randrange100, benchmark::PriorityLevel::HIGH);
114+
BENCHMARK(InsecureRandom_randrange1000, benchmark::PriorityLevel::HIGH);
115+
BENCHMARK(InsecureRandom_randrange1000000, benchmark::PriorityLevel::HIGH);
116+
BENCHMARK(InsecureRandom_Shuffle100, benchmark::PriorityLevel::HIGH);
117+
BENCHMARK(InsecureRandom_stdshuffle100, benchmark::PriorityLevel::HIGH);

0 commit comments

Comments
 (0)