Bitcoin Core
27.99.0
P2P Digital Currency
src
bench
parse_hex.cpp
Go to the documentation of this file.
1
// Copyright (c) 2024- 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
#include <stddef.h>
8
#include <
util/strencodings.h
>
9
#include <cassert>
10
#include <optional>
11
#include <vector>
12
13
std::string
generateHexString
(
size_t
length) {
14
const
auto
hex_digits =
"0123456789ABCDEF"
;
15
FastRandomContext
rng(
/*fDeterministic=*/
true
);
16
17
std::string data;
18
while
(data.size() < length) {
19
auto
digit = hex_digits[rng.
randbits
(4)];
20
data.push_back(digit);
21
}
22
return
data;
23
}
24
25
static
void
HexParse
(
benchmark::Bench
& bench)
26
{
27
auto
data =
generateHexString
(130);
// Generates 678B0EDA0A1FD30904D5A65E3568DB82DB2D918B0AD8DEA18A63FECCB877D07CAD1495C7157584D877420EF38B8DA473A6348B4F51811AC13C786B962BEE5668F9 by default
28
29
bench.
batch
(data.size()).
unit
(
"base16"
).
run
([&] {
30
auto
result =
TryParseHex
(data);
31
assert
(result != std::nullopt);
// make sure we're measuring the successful case
32
ankerl::nanobench::doNotOptimizeAway
(result);
33
});
34
}
35
36
BENCHMARK
(
HexParse
,
benchmark::PriorityLevel::HIGH
);
bench.h
FastRandomContext
Fast randomness source.
Definition:
random.h:145
FastRandomContext::randbits
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition:
random.h:185
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition:
nanobench.h:627
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition:
nanobench.h:1234
ankerl::nanobench::Bench::batch
Bench & batch(T b) noexcept
Sets the batch size.
Definition:
nanobench.h:1258
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
ankerl::nanobench::doNotOptimizeAway
void doNotOptimizeAway(Arg &&arg)
Makes sure none of the given arguments are optimized away by the compiler.
Definition:
nanobench.h:1279
benchmark::HIGH
@ HIGH
Definition:
bench.h:47
generateHexString
std::string generateHexString(size_t length)
Definition:
parse_hex.cpp:13
HexParse
static void HexParse(benchmark::Bench &bench)
Definition:
parse_hex.cpp:25
BENCHMARK
BENCHMARK(HexParse, benchmark::PriorityLevel::HIGH)
strencodings.h
TryParseHex
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
Definition:
strencodings.cpp:81
assert
assert(!tx.IsCoinBase())
Generated on Sun Apr 28 2024 20:02:25 for Bitcoin Core by
1.9.1