ascii_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/ascii.h"
16 
17 #include <cctype>
18 #include <string>
19 #include <array>
20 #include <random>
21 
22 #include "benchmark/benchmark.h"
23 
24 namespace {
25 
26 std::array<unsigned char, 256> MakeShuffledBytes() {
27  std::array<unsigned char, 256> bytes;
28  for (size_t i = 0; i < 256; ++i) bytes[i] = static_cast<unsigned char>(i);
29  std::random_device rd;
30  std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
31  std::mt19937 g(seed);
32  std::shuffle(bytes.begin(), bytes.end(), g);
33  return bytes;
34 }
35 
36 template <typename Function>
37 void AsciiBenchmark(benchmark::State& state, Function f) {
38  std::array<unsigned char, 256> bytes = MakeShuffledBytes();
39  size_t sum = 0;
40  for (auto _ : state) {
41  for (unsigned char b : bytes) sum += f(b) ? 1 : 0;
42  }
43  // Make a copy of `sum` before calling `DoNotOptimize` to make sure that `sum`
44  // can be put in a CPU register and not degrade performance in the loop above.
45  size_t sum2 = sum;
46  benchmark::DoNotOptimize(sum2);
47  state.SetBytesProcessed(state.iterations() * bytes.size());
48 }
49 
50 using StdAsciiFunction = int (*)(int);
51 template <StdAsciiFunction f>
52 void BM_Ascii(benchmark::State& state) {
53  AsciiBenchmark(state, f);
54 }
55 
56 using AbslAsciiIsFunction = bool (*)(unsigned char);
57 template <AbslAsciiIsFunction f>
58 void BM_Ascii(benchmark::State& state) {
59  AsciiBenchmark(state, f);
60 }
61 
62 using AbslAsciiToFunction = char (*)(unsigned char);
63 template <AbslAsciiToFunction f>
64 void BM_Ascii(benchmark::State& state) {
65  AsciiBenchmark(state, f);
66 }
67 
68 inline char Noop(unsigned char b) { return static_cast<char>(b); }
69 
70 BENCHMARK_TEMPLATE(BM_Ascii, Noop);
71 BENCHMARK_TEMPLATE(BM_Ascii, std::isalpha);
72 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalpha);
73 BENCHMARK_TEMPLATE(BM_Ascii, std::isdigit);
74 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isdigit);
75 BENCHMARK_TEMPLATE(BM_Ascii, std::isalnum);
76 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalnum);
77 BENCHMARK_TEMPLATE(BM_Ascii, std::isspace);
78 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isspace);
79 BENCHMARK_TEMPLATE(BM_Ascii, std::ispunct);
80 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_ispunct);
81 BENCHMARK_TEMPLATE(BM_Ascii, std::isblank);
82 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isblank);
83 BENCHMARK_TEMPLATE(BM_Ascii, std::iscntrl);
84 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_iscntrl);
85 BENCHMARK_TEMPLATE(BM_Ascii, std::isxdigit);
86 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isxdigit);
87 BENCHMARK_TEMPLATE(BM_Ascii, std::isprint);
88 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isprint);
89 BENCHMARK_TEMPLATE(BM_Ascii, std::isgraph);
90 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isgraph);
91 BENCHMARK_TEMPLATE(BM_Ascii, std::isupper);
92 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isupper);
93 BENCHMARK_TEMPLATE(BM_Ascii, std::islower);
94 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_islower);
95 BENCHMARK_TEMPLATE(BM_Ascii, isascii);
96 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isascii);
97 BENCHMARK_TEMPLATE(BM_Ascii, std::tolower);
98 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_tolower);
99 BENCHMARK_TEMPLATE(BM_Ascii, std::toupper);
100 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_toupper);
101 
102 static void BM_StrToLower(benchmark::State& state) {
103  const int size = state.range(0);
104  std::string s(size, 'X');
105  for (auto _ : state) {
106  benchmark::DoNotOptimize(absl::AsciiStrToLower(s));
107  }
108 }
109 BENCHMARK(BM_StrToLower)->Range(1, 1 << 20);
110 
111 static void BM_StrToUpper(benchmark::State& state) {
112  const int size = state.range(0);
113  std::string s(size, 'x');
114  for (auto _ : state) {
115  benchmark::DoNotOptimize(absl::AsciiStrToUpper(s));
116  }
117 }
118 BENCHMARK(BM_StrToUpper)->Range(1, 1 << 20);
119 
120 } // namespace
bool ascii_isalnum(unsigned char c)
Definition: ascii.h:85
void AsciiStrToLower(std::string *s)
Definition: ascii.cc:157
bool ascii_isblank(unsigned char c)
Definition: ascii.h:107
bool ascii_isgraph(unsigned char c)
Definition: ascii.h:140
bool ascii_isprint(unsigned char c)
Definition: ascii.h:135
bool ascii_isspace(unsigned char c)
Definition: ascii.h:93
bool ascii_isalpha(unsigned char c)
Definition: ascii.h:78
void AsciiStrToUpper(std::string *s)
Definition: ascii.cc:163
char ascii_toupper(unsigned char c)
Definition: ascii.h:179
bool ascii_isupper(unsigned char c)
Definition: ascii.h:145
uintptr_t size
bool ascii_islower(unsigned char c)
Definition: ascii.h:150
bool ascii_ispunct(unsigned char c)
Definition: ascii.h:100
bool ascii_isascii(unsigned char c)
Definition: ascii.h:155
bool ascii_isxdigit(unsigned char c)
Definition: ascii.h:122
bool ascii_isdigit(unsigned char c)
Definition: ascii.h:130
char ascii_tolower(unsigned char c)
Definition: ascii.h:161
uint64_t b
Definition: layout_test.cc:50
bool ascii_iscntrl(unsigned char c)
Definition: ascii.h:114


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:17