| 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 <algorithm> |
| 18 | #include <cctype> |
| 19 | #include <cstddef> |
| 20 | #include <string> |
| 21 | #include <array> |
| 22 | #include <random> |
| 23 |
| 24 | #include "benchmark/benchmark.h" |
| 25 |
| 26 | namespace { |
| 27 |
| 28 | std::array<unsigned char, 256> MakeShuffledBytes() { |
| 29 | std::array<unsigned char, 256> bytes; |
| 30 | for (size_t i = 0; i < 256; ++i) bytes[i] = static_cast<unsigned char>(i); |
| 31 | std::random_device rd; |
| 32 | std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()}); |
| 33 | std::mt19937 g(seed); |
| 34 | std::shuffle(bytes.begin(), bytes.end(), g); |
| 35 | return bytes; |
| 36 | } |
| 37 |
| 38 | template <typename Function> |
| 39 | void AsciiBenchmark(benchmark::State& state, Function f) { |
| 40 | std::array<unsigned char, 256> bytes = MakeShuffledBytes(); |
| 41 | size_t sum = 0; |
| 42 | for (auto _ : state) { |
| 43 | for (unsigned char b : bytes) sum += f(b) ? 1 : 0; |
| 44 | } |
| 45 | // Make a copy of `sum` before calling `DoNotOptimize` to make sure that `sum` |
| 46 | // can be put in a CPU register and not degrade performance in the loop above. |
| 47 | size_t sum2 = sum; |
| 48 | benchmark::DoNotOptimize(sum2); |
| 49 | state.SetBytesProcessed(state.iterations() * bytes.size()); |
| 50 | } |
| 51 |
| 52 | using StdAsciiFunction = int (*)(int); |
| 53 | template <StdAsciiFunction f> |
| 54 | void BM_Ascii(benchmark::State& state) { |
| 55 | AsciiBenchmark(state, f); |
| 56 | } |
| 57 |
| 58 | using AbslAsciiIsFunction = bool (*)(unsigned char); |
| 59 | template <AbslAsciiIsFunction f> |
| 60 | void BM_Ascii(benchmark::State& state) { |
| 61 | AsciiBenchmark(state, f); |
| 62 | } |
| 63 |
| 64 | using AbslAsciiToFunction = char (*)(unsigned char); |
| 65 | template <AbslAsciiToFunction f> |
| 66 | void BM_Ascii(benchmark::State& state) { |
| 67 | AsciiBenchmark(state, f); |
| 68 | } |
| 69 |
| 70 | inline char Noop(unsigned char b) { return static_cast<char>(b); } |
| 71 |
| 72 | BENCHMARK_TEMPLATE(BM_Ascii, Noop); |
| 73 | BENCHMARK_TEMPLATE(BM_Ascii, std::isalpha); |
| 74 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalpha); |
| 75 | BENCHMARK_TEMPLATE(BM_Ascii, std::isdigit); |
| 76 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isdigit); |
| 77 | BENCHMARK_TEMPLATE(BM_Ascii, std::isalnum); |
| 78 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalnum); |
| 79 | BENCHMARK_TEMPLATE(BM_Ascii, std::isspace); |
| 80 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isspace); |
| 81 | BENCHMARK_TEMPLATE(BM_Ascii, std::ispunct); |
| 82 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_ispunct); |
| 83 | BENCHMARK_TEMPLATE(BM_Ascii, std::isblank); |
| 84 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isblank); |
| 85 | BENCHMARK_TEMPLATE(BM_Ascii, std::iscntrl); |
| 86 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_iscntrl); |
| 87 | BENCHMARK_TEMPLATE(BM_Ascii, std::isxdigit); |
| 88 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isxdigit); |
| 89 | BENCHMARK_TEMPLATE(BM_Ascii, std::isprint); |
| 90 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isprint); |
| 91 | BENCHMARK_TEMPLATE(BM_Ascii, std::isgraph); |
| 92 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isgraph); |
| 93 | BENCHMARK_TEMPLATE(BM_Ascii, std::isupper); |
| 94 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isupper); |
| 95 | BENCHMARK_TEMPLATE(BM_Ascii, std::islower); |
| 96 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_islower); |
| 97 | BENCHMARK_TEMPLATE(BM_Ascii, isascii); |
| 98 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isascii); |
| 99 | BENCHMARK_TEMPLATE(BM_Ascii, std::tolower); |
| 100 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_tolower); |
| 101 | BENCHMARK_TEMPLATE(BM_Ascii, std::toupper); |
| 102 | BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_toupper); |
| 103 |
| 104 | static void BM_StrToLower(benchmark::State& state) { |
| 105 | const size_t size = static_cast<size_t>(state.range(0)); |
| 106 | std::string s(size, 'X'); |
| 107 | for (auto _ : state) { |
| 108 | benchmark::DoNotOptimize(s); |
| 109 | std::string res = absl::AsciiStrToLower(s); |
| 110 | benchmark::DoNotOptimize(res); |
| 111 | } |
| 112 | } |
| 113 | BENCHMARK(BM_StrToLower) |
| 114 | ->DenseRange(0, 32) |
| 115 | ->RangeMultiplier(2) |
| 116 | ->Range(64, 1 << 26); |
| 117 |
| 118 | static void BM_StrToUpper(benchmark::State& state) { |
| 119 | const size_t size = static_cast<size_t>(state.range(0)); |
| 120 | std::string s(size, 'x'); |
| 121 | for (auto _ : state) { |
| 122 | benchmark::DoNotOptimize(s); |
| 123 | std::string res = absl::AsciiStrToUpper(s); |
| 124 | benchmark::DoNotOptimize(res); |
| 125 | } |
| 126 | } |
| 127 | BENCHMARK(BM_StrToUpper) |
| 128 | ->DenseRange(0, 32) |
| 129 | ->RangeMultiplier(2) |
| 130 | ->Range(64, 1 << 26); |
| 131 |
| 132 | static void BM_StrToUpperFromRvalref(benchmark::State& state) { |
| 133 | const size_t size = static_cast<size_t>(state.range(0)); |
| 134 | std::string s(size, 'X'); |
| 135 | for (auto _ : state) { |
| 136 | benchmark::DoNotOptimize(s); |
| 137 | std::string res = absl::AsciiStrToUpper(std::string(s)); |
| 138 | benchmark::DoNotOptimize(res); |
| 139 | } |
| 140 | } |
| 141 | BENCHMARK(BM_StrToUpperFromRvalref) |
| 142 | ->DenseRange(0, 32) |
| 143 | ->RangeMultiplier(2) |
| 144 | ->Range(64, 1 << 26); |
| 145 |
| 146 | static void BM_StrToLowerFromRvalref(benchmark::State& state) { |
| 147 | const size_t size = static_cast<size_t>(state.range(0)); |
| 148 | std::string s(size, 'x'); |
| 149 | for (auto _ : state) { |
| 150 | benchmark::DoNotOptimize(s); |
| 151 | std::string res = absl::AsciiStrToLower(std::string(s)); |
| 152 | benchmark::DoNotOptimize(res); |
| 153 | } |
| 154 | } |
| 155 | BENCHMARK(BM_StrToLowerFromRvalref) |
| 156 | ->DenseRange(0, 32) |
| 157 | ->RangeMultiplier(2) |
| 158 | ->Range(64, 1 << 26); |
| 159 |
| 160 | } // namespace |
| 161 |