00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/ascii.h"
00016
00017 #include <cctype>
00018 #include <string>
00019 #include <array>
00020 #include <random>
00021
00022 #include "benchmark/benchmark.h"
00023
00024 namespace {
00025
00026 std::array<unsigned char, 256> MakeShuffledBytes() {
00027 std::array<unsigned char, 256> bytes;
00028 for (size_t i = 0; i < 256; ++i) bytes[i] = static_cast<unsigned char>(i);
00029 std::random_device rd;
00030 std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
00031 std::mt19937 g(seed);
00032 std::shuffle(bytes.begin(), bytes.end(), g);
00033 return bytes;
00034 }
00035
00036 template <typename Function>
00037 void AsciiBenchmark(benchmark::State& state, Function f) {
00038 std::array<unsigned char, 256> bytes = MakeShuffledBytes();
00039 size_t sum = 0;
00040 for (auto _ : state) {
00041 for (unsigned char b : bytes) sum += f(b) ? 1 : 0;
00042 }
00043
00044
00045 size_t sum2 = sum;
00046 benchmark::DoNotOptimize(sum2);
00047 state.SetBytesProcessed(state.iterations() * bytes.size());
00048 }
00049
00050 using StdAsciiFunction = int (*)(int);
00051 template <StdAsciiFunction f>
00052 void BM_Ascii(benchmark::State& state) {
00053 AsciiBenchmark(state, f);
00054 }
00055
00056 using AbslAsciiIsFunction = bool (*)(unsigned char);
00057 template <AbslAsciiIsFunction f>
00058 void BM_Ascii(benchmark::State& state) {
00059 AsciiBenchmark(state, f);
00060 }
00061
00062 using AbslAsciiToFunction = char (*)(unsigned char);
00063 template <AbslAsciiToFunction f>
00064 void BM_Ascii(benchmark::State& state) {
00065 AsciiBenchmark(state, f);
00066 }
00067
00068 inline char Noop(unsigned char b) { return static_cast<char>(b); }
00069
00070 BENCHMARK_TEMPLATE(BM_Ascii, Noop);
00071 BENCHMARK_TEMPLATE(BM_Ascii, std::isalpha);
00072 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalpha);
00073 BENCHMARK_TEMPLATE(BM_Ascii, std::isdigit);
00074 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isdigit);
00075 BENCHMARK_TEMPLATE(BM_Ascii, std::isalnum);
00076 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isalnum);
00077 BENCHMARK_TEMPLATE(BM_Ascii, std::isspace);
00078 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isspace);
00079 BENCHMARK_TEMPLATE(BM_Ascii, std::ispunct);
00080 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_ispunct);
00081 BENCHMARK_TEMPLATE(BM_Ascii, std::isblank);
00082 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isblank);
00083 BENCHMARK_TEMPLATE(BM_Ascii, std::iscntrl);
00084 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_iscntrl);
00085 BENCHMARK_TEMPLATE(BM_Ascii, std::isxdigit);
00086 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isxdigit);
00087 BENCHMARK_TEMPLATE(BM_Ascii, std::isprint);
00088 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isprint);
00089 BENCHMARK_TEMPLATE(BM_Ascii, std::isgraph);
00090 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isgraph);
00091 BENCHMARK_TEMPLATE(BM_Ascii, std::isupper);
00092 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isupper);
00093 BENCHMARK_TEMPLATE(BM_Ascii, std::islower);
00094 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_islower);
00095 BENCHMARK_TEMPLATE(BM_Ascii, isascii);
00096 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_isascii);
00097 BENCHMARK_TEMPLATE(BM_Ascii, std::tolower);
00098 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_tolower);
00099 BENCHMARK_TEMPLATE(BM_Ascii, std::toupper);
00100 BENCHMARK_TEMPLATE(BM_Ascii, absl::ascii_toupper);
00101
00102 static void BM_StrToLower(benchmark::State& state) {
00103 const int size = state.range(0);
00104 std::string s(size, 'X');
00105 for (auto _ : state) {
00106 benchmark::DoNotOptimize(absl::AsciiStrToLower(s));
00107 }
00108 }
00109 BENCHMARK(BM_StrToLower)->Range(1, 1 << 20);
00110
00111 static void BM_StrToUpper(benchmark::State& state) {
00112 const int size = state.range(0);
00113 std::string s(size, 'x');
00114 for (auto _ : state) {
00115 benchmark::DoNotOptimize(absl::AsciiStrToUpper(s));
00116 }
00117 }
00118 BENCHMARK(BM_StrToUpper)->Range(1, 1 << 20);
00119
00120 }