|
| 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 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +template<typename RNG> |
| 14 | +void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept |
| 15 | +{ |
| 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([&] { |
| 25 | + rng.rand32(); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +template<typename RNG> |
| 30 | +void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept |
| 31 | +{ |
| 32 | + bench.batch(1).unit("number").run([&] { |
| 33 | + rng.randbool(); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 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_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 | + |
| 67 | +void FastRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, FastRandomContext(true)); } |
| 68 | +void FastRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, FastRandomContext(true)); } |
| 69 | +void FastRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, FastRandomContext(true)); } |
| 70 | +void FastRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, FastRandomContext(true)); } |
| 71 | +void FastRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, FastRandomContext(true)); } |
| 72 | +void FastRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, FastRandomContext(true)); } |
| 73 | +void FastRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, FastRandomContext(true)); } |
| 74 | +void FastRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, FastRandomContext(true)); } |
| 75 | + |
| 76 | +void InsecureRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, InsecureRandomContext(251438)); } |
| 77 | +void InsecureRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, InsecureRandomContext(251438)); } |
| 78 | +void InsecureRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, InsecureRandomContext(251438)); } |
| 79 | +void InsecureRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, InsecureRandomContext(251438)); } |
| 80 | +void InsecureRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, InsecureRandomContext(251438)); } |
| 81 | +void InsecureRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, InsecureRandomContext(251438)); } |
| 82 | +void InsecureRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, InsecureRandomContext(251438)); } |
| 83 | +void InsecureRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, InsecureRandomContext(251438)); } |
| 84 | + |
| 85 | +} // namespace |
| 86 | + |
| 87 | +BENCHMARK(FastRandom_rand64, benchmark::PriorityLevel::HIGH); |
| 88 | +BENCHMARK(FastRandom_rand32, benchmark::PriorityLevel::HIGH); |
| 89 | +BENCHMARK(FastRandom_randbool, benchmark::PriorityLevel::HIGH); |
| 90 | +BENCHMARK(FastRandom_randbits, benchmark::PriorityLevel::HIGH); |
| 91 | +BENCHMARK(FastRandom_randrange100, benchmark::PriorityLevel::HIGH); |
| 92 | +BENCHMARK(FastRandom_randrange1000, benchmark::PriorityLevel::HIGH); |
| 93 | +BENCHMARK(FastRandom_randrange1000000, benchmark::PriorityLevel::HIGH); |
| 94 | +BENCHMARK(FastRandom_stdshuffle100, benchmark::PriorityLevel::HIGH); |
| 95 | + |
| 96 | +BENCHMARK(InsecureRandom_rand64, benchmark::PriorityLevel::HIGH); |
| 97 | +BENCHMARK(InsecureRandom_rand32, benchmark::PriorityLevel::HIGH); |
| 98 | +BENCHMARK(InsecureRandom_randbool, benchmark::PriorityLevel::HIGH); |
| 99 | +BENCHMARK(InsecureRandom_randbits, benchmark::PriorityLevel::HIGH); |
| 100 | +BENCHMARK(InsecureRandom_randrange100, benchmark::PriorityLevel::HIGH); |
| 101 | +BENCHMARK(InsecureRandom_randrange1000, benchmark::PriorityLevel::HIGH); |
| 102 | +BENCHMARK(InsecureRandom_randrange1000000, benchmark::PriorityLevel::HIGH); |
| 103 | +BENCHMARK(InsecureRandom_stdshuffle100, benchmark::PriorityLevel::HIGH); |
0 commit comments