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
18template <class CharT>
19static 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
29BENCHMARK(BM_tolower_char<char>);
30#ifndef TEST_HAS_NO_WIDE_CHARACTERS
31BENCHMARK(BM_tolower_char<wchar_t>);
32#endif
33
34template <class CharT>
35static 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
46BENCHMARK(BM_tolower_string<char>);
47#ifndef TEST_HAS_NO_WIDE_CHARACTERS
48BENCHMARK(BM_tolower_string<wchar_t>);
49#endif
50
51template <class CharT>
52static 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
60BENCHMARK(BM_toupper_char<char>);
61#ifndef TEST_HAS_NO_WIDE_CHARACTERS
62BENCHMARK(BM_toupper_char<wchar_t>);
63#endif
64
65template <class CharT>
66static 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
77BENCHMARK(BM_toupper_string<char>);
78#ifndef TEST_HAS_NO_WIDE_CHARACTERS
79BENCHMARK(BM_toupper_string<wchar_t>);
80#endif
81
82BENCHMARK_MAIN();
83

source code of libcxx/test/benchmarks/locale/ctype.bench.cpp