ascii_benchmark.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "absl/strings/ascii.h"
  15. #include <cctype>
  16. #include <string>
  17. #include <array>
  18. #include <random>
  19. #include "benchmark/benchmark.h"
  20. namespace {
  21. std::array<unsigned char, 256> MakeShuffledBytes() {
  22. std::array<unsigned char, 256> bytes;
  23. for (size_t i = 0; i < 256; ++i) bytes[i] = static_cast<unsigned char>(i);
  24. std::random_device rd;
  25. std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
  26. std::mt19937 g(seed);
  27. std::shuffle(bytes.begin(), bytes.end(), g);
  28. return bytes;
  29. }
  30. template <typename Function>
  31. void AsciiBenchmark(benchmark::State& state, Function f) {
  32. std::array<unsigned char, 256> bytes = MakeShuffledBytes();
  33. size_t sum = 0;
  34. for (auto _ : state) {
  35. for (unsigned char b : bytes) sum += f(b) ? 1 : 0;
  36. }
  37. // Make a copy of `sum` before calling `DoNotOptimize` to make sure that `sum`
  38. // can be put in a CPU register and not degrade performance in the loop above.
  39. size_t sum2 = sum;
  40. benchmark::DoNotOptimize(sum2);
  41. state.SetBytesProcessed(state.iterations() * bytes.size());
  42. }
  43. using StdAsciiFunction = int (*)(int);
  44. template <StdAsciiFunction f>
  45. void BM_Ascii(benchmark::State& state) {
  46. AsciiBenchmark(state, f);
  47. }
  48. using AbslAsciiIsFunction = bool (*)(unsigned char);
  49. template <AbslAsciiIsFunction f>
  50. void BM_Ascii(benchmark::State& state) {
  51. AsciiBenchmark(state, f);
  52. }
  53. using AbslAsciiToFunction = char (*)(unsigned char);
  54. template <AbslAsciiToFunction f>
  55. void BM_Ascii(benchmark::State& state) {
  56. AsciiBenchmark(state, f);
  57. }
  58. inline char Noop(unsigned char b) { return static_cast<char>(b); }
  59. BENCHMARK_TEMPLATE(BM_Ascii, Noop);
  60. BENCHMARK_TEMPLATE(BM_Ascii, std::isalpha);
  61. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalpha);
  62. BENCHMARK_TEMPLATE(BM_Ascii, std::isdigit);
  63. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isdigit);
  64. BENCHMARK_TEMPLATE(BM_Ascii, std::isalnum);
  65. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalnum);
  66. BENCHMARK_TEMPLATE(BM_Ascii, std::isspace);
  67. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isspace);
  68. BENCHMARK_TEMPLATE(BM_Ascii, std::ispunct);
  69. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_ispunct);
  70. BENCHMARK_TEMPLATE(BM_Ascii, std::isblank);
  71. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isblank);
  72. BENCHMARK_TEMPLATE(BM_Ascii, std::iscntrl);
  73. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_iscntrl);
  74. BENCHMARK_TEMPLATE(BM_Ascii, std::isxdigit);
  75. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isxdigit);
  76. BENCHMARK_TEMPLATE(BM_Ascii, std::isprint);
  77. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isprint);
  78. BENCHMARK_TEMPLATE(BM_Ascii, std::isgraph);
  79. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isgraph);
  80. BENCHMARK_TEMPLATE(BM_Ascii, std::isupper);
  81. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isupper);
  82. BENCHMARK_TEMPLATE(BM_Ascii, std::islower);
  83. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_islower);
  84. BENCHMARK_TEMPLATE(BM_Ascii, isascii);
  85. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isascii);
  86. BENCHMARK_TEMPLATE(BM_Ascii, std::tolower);
  87. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_tolower);
  88. BENCHMARK_TEMPLATE(BM_Ascii, std::toupper);
  89. BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_toupper);
  90. static void BM_StrToLower(benchmark::State& state) {
  91. const int size = state.range(0);
  92. std::string s(size, 'X');
  93. for (auto _ : state) {
  94. benchmark::DoNotOptimize(absl::AsciiStrToLower(s));
  95. }
  96. }
  97. BENCHMARK(BM_StrToLower)->Range(1, 1 << 20);
  98. static void BM_StrToUpper(benchmark::State& state) {
  99. const int size = state.range(0);
  100. std::string s(size, 'x');
  101. for (auto _ : state) {
  102. benchmark::DoNotOptimize(absl::AsciiStrToUpper(s));
  103. }
  104. }
  105. BENCHMARK(BM_StrToUpper)->Range(1, 1 << 20);
  106. } // namespace