22 #include "benchmark/benchmark.h" 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()});
32 std::shuffle(bytes.begin(), bytes.end(), g);
36 template <
typename Function>
37 void AsciiBenchmark(benchmark::State& state, Function f) {
38 std::array<unsigned char, 256> bytes = MakeShuffledBytes();
40 for (
auto _ : state) {
41 for (
unsigned char b : bytes) sum += f(
b) ? 1 : 0;
46 benchmark::DoNotOptimize(sum2);
47 state.SetBytesProcessed(state.iterations() * bytes.size());
50 using StdAsciiFunction = int (*)(int);
51 template <StdAsciiFunction f>
52 void BM_Ascii(benchmark::State& state) {
53 AsciiBenchmark(state, f);
56 using AbslAsciiIsFunction =
bool (*)(
unsigned char);
57 template <AbslAsciiIsFunction f>
58 void BM_Ascii(benchmark::State& state) {
59 AsciiBenchmark(state, f);
62 using AbslAsciiToFunction = char (*)(
unsigned char);
63 template <AbslAsciiToFunction f>
64 void BM_Ascii(benchmark::State& state) {
65 AsciiBenchmark(state, f);
68 inline char Noop(
unsigned char b) {
return static_cast<char>(
b); }
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);
85 BENCHMARK_TEMPLATE(BM_Ascii, std::isxdigit);
87 BENCHMARK_TEMPLATE(BM_Ascii, std::isprint);
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);
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) {
109 BENCHMARK(BM_StrToLower)->Range(1, 1 << 20);
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) {
118 BENCHMARK(BM_StrToUpper)->Range(1, 1 << 20);
bool ascii_isalnum(unsigned char c)
void AsciiStrToLower(std::string *s)
bool ascii_isblank(unsigned char c)
bool ascii_isgraph(unsigned char c)
bool ascii_isprint(unsigned char c)
bool ascii_isspace(unsigned char c)
bool ascii_isalpha(unsigned char c)
void AsciiStrToUpper(std::string *s)
char ascii_toupper(unsigned char c)
bool ascii_isupper(unsigned char c)
bool ascii_islower(unsigned char c)
bool ascii_ispunct(unsigned char c)
bool ascii_isascii(unsigned char c)
bool ascii_isxdigit(unsigned char c)
bool ascii_isdigit(unsigned char c)
char ascii_tolower(unsigned char c)
bool ascii_iscntrl(unsigned char c)