bloaty/third_party/abseil-cpp/absl/strings/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;
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);
73 BENCHMARK_TEMPLATE(BM_Ascii, std::isdigit);
75 BENCHMARK_TEMPLATE(BM_Ascii, std::isalnum);
77 BENCHMARK_TEMPLATE(BM_Ascii, std::isspace);
79 BENCHMARK_TEMPLATE(BM_Ascii, std::ispunct);
81 BENCHMARK_TEMPLATE(BM_Ascii, std::isblank);
83 BENCHMARK_TEMPLATE(BM_Ascii, std::iscntrl);
89 BENCHMARK_TEMPLATE(BM_Ascii, std::isgraph);
91 BENCHMARK_TEMPLATE(BM_Ascii, std::isupper);
93 BENCHMARK_TEMPLATE(BM_Ascii, std::islower);
95 BENCHMARK_TEMPLATE(BM_Ascii, isascii);
97 BENCHMARK_TEMPLATE(BM_Ascii, std::tolower);
99 BENCHMARK_TEMPLATE(BM_Ascii, std::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) {
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) {
116  }
117 }
118 BENCHMARK(BM_StrToUpper)->Range(1, 1 << 20);
119 
120 } // namespace
absl::ascii_isblank
bool ascii_isblank(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:109
absl::ascii_tolower
char ascii_tolower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:163
bool
bool
Definition: setup_once.h:312
grpc::testing::sum
double sum(const T &container, F functor)
Definition: test/cpp/qps/stats.h:30
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::ascii_iscntrl
bool ascii_iscntrl(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:116
absl::ascii_isgraph
bool ascii_isgraph(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:142
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
absl::FormatConversionChar::s
@ s
absl::AsciiStrToLower
void AsciiStrToLower(std::string *s)
Definition: abseil-cpp/absl/strings/ascii.cc:158
absl::ascii_isspace
bool ascii_isspace(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:95
absl::ascii_isalnum
bool ascii_isalnum(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:87
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
xds_interop_client.int
int
Definition: xds_interop_client.py:113
absl::ascii_isascii
bool ascii_isascii(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:157
absl::AsciiStrToUpper
void AsciiStrToUpper(std::string *s)
Definition: abseil-cpp/absl/strings/ascii.cc:164
google::protobuf::isprint
bool isprint(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:79
BENCHMARK_TEMPLATE
#define BENCHMARK_TEMPLATE(n, a)
Definition: benchmark/include/benchmark/benchmark.h:1231
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
absl::ascii_isprint
bool ascii_isprint(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:137
absl::ascii_islower
bool ascii_islower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:152
absl::ascii_isupper
bool ascii_isupper(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:147
absl::ascii_isalpha
bool ascii_isalpha(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:80
benchmark::internal::Function
void() Function(State &)
Definition: benchmark/include/benchmark/benchmark.h:826
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
google::protobuf::isxdigit
bool isxdigit(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:73
g
struct @717 g
absl::ascii_toupper
char ascii_toupper(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:181
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
absl::ascii_ispunct
bool ascii_ispunct(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:102
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
BENCHMARK
#define BENCHMARK(n)
Definition: benchmark/include/benchmark/benchmark.h:1170
absl::ascii_isxdigit
bool ascii_isxdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:124
absl::ascii_isdigit
bool ascii_isdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:132
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:44