Skip to content

Commit 8596c6e

Browse files
derekmaurocopybara-github
authored andcommitted
Add benchmark for absl::HexStringToBytes
PiperOrigin-RevId: 692960029 Change-Id: I98252f2de95a237622ccf16f200bbb16dc441d88
1 parent 85c26be commit 8596c6e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

absl/strings/escaping_benchmark.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ void BM_WebSafeBase64Escape_string(benchmark::State& state) {
5858
}
5959
BENCHMARK(BM_WebSafeBase64Escape_string);
6060

61+
void BM_HexStringToBytes(benchmark::State& state) {
62+
const int size = state.range(0);
63+
std::string input, output;
64+
for (int i = 0; i < size; ++i) input += "1c";
65+
for (auto _ : state) {
66+
benchmark::DoNotOptimize(input);
67+
bool result = absl::HexStringToBytes(input, &output);
68+
benchmark::DoNotOptimize(result);
69+
benchmark::DoNotOptimize(output);
70+
}
71+
}
72+
BENCHMARK(BM_HexStringToBytes)->Range(1, 1 << 8);
73+
74+
void BM_HexStringToBytes_Fail(benchmark::State& state) {
75+
std::string binary;
76+
absl::string_view hex_input1 = "1c2f003";
77+
absl::string_view hex_input2 = "1c2f0032f40123456789abcdef**";
78+
for (auto _ : state) {
79+
benchmark::DoNotOptimize(hex_input1);
80+
bool result1 = absl::HexStringToBytes(hex_input1, &binary);
81+
benchmark::DoNotOptimize(result1);
82+
benchmark::DoNotOptimize(binary);
83+
benchmark::DoNotOptimize(hex_input2);
84+
bool result2 = absl::HexStringToBytes(hex_input2, &binary);
85+
benchmark::DoNotOptimize(result2);
86+
benchmark::DoNotOptimize(binary);
87+
}
88+
}
89+
BENCHMARK(BM_HexStringToBytes_Fail);
90+
6191
// Used for the CEscape benchmarks
6292
const char kStringValueNoEscape[] = "1234567890";
6393
const char kStringValueSomeEscaped[] = "123\n56789\xA1";

0 commit comments

Comments
 (0)