| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14 |
| 10 | |
| 11 | #include <locale> |
| 12 | |
| 13 | #include <benchmark/benchmark.h> |
| 14 | |
| 15 | #include "make_string.h" |
| 16 | #include "test_macros.h" |
| 17 | |
| 18 | template <class CharT> |
| 19 | static void BM_tolower_char(benchmark::State& state) { |
| 20 | const auto& ct = std::use_facet<std::ctype<CharT>>(std::locale::classic()); |
| 21 | |
| 22 | for (auto _ : state) { |
| 23 | CharT c('c'); |
| 24 | benchmark::DoNotOptimize(c); |
| 25 | benchmark::DoNotOptimize(ct.tolower(c)); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | BENCHMARK(BM_tolower_char<char>); |
| 30 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 31 | BENCHMARK(BM_tolower_char<wchar_t>); |
| 32 | #endif |
| 33 | |
| 34 | template <class CharT> |
| 35 | static void BM_tolower_string(benchmark::State& state) { |
| 36 | const auto& ct = std::use_facet<std::ctype<CharT>>(std::locale::classic()); |
| 37 | std::basic_string<CharT> str; |
| 38 | |
| 39 | for (auto _ : state) { |
| 40 | str = MAKE_STRING_VIEW(CharT, "THIS IS A LONG STRING TO MAKE TO LOWER" ); |
| 41 | benchmark::DoNotOptimize(str); |
| 42 | benchmark::DoNotOptimize(ct.tolower(str.data(), str.data() + str.size())); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | BENCHMARK(BM_tolower_string<char>); |
| 47 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 48 | BENCHMARK(BM_tolower_string<wchar_t>); |
| 49 | #endif |
| 50 | |
| 51 | template <class CharT> |
| 52 | static void BM_toupper_char(benchmark::State& state) { |
| 53 | const auto& ct = std::use_facet<std::ctype<CharT>>(std::locale::classic()); |
| 54 | |
| 55 | for (auto _ : state) { |
| 56 | benchmark::DoNotOptimize(ct.toupper(CharT('c'))); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | BENCHMARK(BM_toupper_char<char>); |
| 61 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 62 | BENCHMARK(BM_toupper_char<wchar_t>); |
| 63 | #endif |
| 64 | |
| 65 | template <class CharT> |
| 66 | static void BM_toupper_string(benchmark::State& state) { |
| 67 | const auto& ct = std::use_facet<std::ctype<CharT>>(std::locale::classic()); |
| 68 | std::basic_string<CharT> str; |
| 69 | |
| 70 | for (auto _ : state) { |
| 71 | str = MAKE_STRING_VIEW(CharT, "this is a long string to make to upper" ); |
| 72 | benchmark::DoNotOptimize(str); |
| 73 | benchmark::DoNotOptimize(ct.toupper(str.data(), str.data() + str.size())); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | BENCHMARK(BM_toupper_string<char>); |
| 78 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 79 | BENCHMARK(BM_toupper_string<wchar_t>); |
| 80 | #endif |
| 81 | |
| 82 | BENCHMARK_MAIN(); |
| 83 | |