xref: /openbsd-src/gnu/llvm/libcxx/benchmarks/format_to.bench.cpp (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert //===----------------------------------------------------------------------===//
2*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
4*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5*4bdff4beSrobert //
6*4bdff4beSrobert //===----------------------------------------------------------------------===//
7*4bdff4beSrobert 
8*4bdff4beSrobert #include <format>
9*4bdff4beSrobert 
10*4bdff4beSrobert #include <iterator>
11*4bdff4beSrobert #include <algorithm>
12*4bdff4beSrobert #include <array>
13*4bdff4beSrobert #include <list>
14*4bdff4beSrobert #include <span>
15*4bdff4beSrobert #include <string>
16*4bdff4beSrobert #include <vector>
17*4bdff4beSrobert 
18*4bdff4beSrobert #include "benchmark/benchmark.h"
19*4bdff4beSrobert #include "make_string.h"
20*4bdff4beSrobert 
21*4bdff4beSrobert #define CSTR(S) MAKE_CSTRING(CharT, S)
22*4bdff4beSrobert 
23*4bdff4beSrobert /*** Back inserter ***/
24*4bdff4beSrobert 
25*4bdff4beSrobert template <class Container>
BM_format_to_string_back_inserter(benchmark::State & state)26*4bdff4beSrobert static void BM_format_to_string_back_inserter(benchmark::State& state) {
27*4bdff4beSrobert   using CharT = typename Container::value_type;
28*4bdff4beSrobert   size_t size = state.range(0);
29*4bdff4beSrobert   auto str = std::basic_string<CharT>(size, CharT('*'));
30*4bdff4beSrobert 
31*4bdff4beSrobert   for (auto _ : state) {
32*4bdff4beSrobert     Container output;
33*4bdff4beSrobert     benchmark::DoNotOptimize(std::format_to(std::back_inserter(output), CSTR("{}"), str));
34*4bdff4beSrobert   }
35*4bdff4beSrobert   state.SetBytesProcessed(state.iterations() * size * sizeof(CharT));
36*4bdff4beSrobert }
37*4bdff4beSrobert 
38*4bdff4beSrobert /*** Begin ***/
39*4bdff4beSrobert 
40*4bdff4beSrobert template <class Container>
BM_format_to_string_begin(benchmark::State & state)41*4bdff4beSrobert static void BM_format_to_string_begin(benchmark::State& state) {
42*4bdff4beSrobert   using CharT = typename Container::value_type;
43*4bdff4beSrobert   size_t size = state.range(0);
44*4bdff4beSrobert   auto str = std::basic_string<CharT>(size, CharT('*'));
45*4bdff4beSrobert 
46*4bdff4beSrobert   Container output(size, CharT('-'));
47*4bdff4beSrobert   for (auto _ : state)
48*4bdff4beSrobert     benchmark::DoNotOptimize(std::format_to(std::begin(output), CSTR("{}"), str));
49*4bdff4beSrobert 
50*4bdff4beSrobert   state.SetBytesProcessed(state.iterations() * size * sizeof(CharT));
51*4bdff4beSrobert }
52*4bdff4beSrobert 
53*4bdff4beSrobert /*** Pointer ***/
54*4bdff4beSrobert 
55*4bdff4beSrobert template <class CharT>
BM_format_to_string_span(benchmark::State & state)56*4bdff4beSrobert static void BM_format_to_string_span(benchmark::State& state) {
57*4bdff4beSrobert   size_t size = state.range(0);
58*4bdff4beSrobert   auto str = std::basic_string<CharT>(size, CharT('*'));
59*4bdff4beSrobert 
60*4bdff4beSrobert   auto buffer = std::basic_string<CharT>(size, CharT('-'));
61*4bdff4beSrobert   std::span<CharT> output{buffer};
62*4bdff4beSrobert   for (auto _ : state)
63*4bdff4beSrobert     benchmark::DoNotOptimize(std::format_to(std::begin(output), CSTR("{}"), str));
64*4bdff4beSrobert 
65*4bdff4beSrobert   state.SetBytesProcessed(state.iterations() * size * sizeof(CharT));
66*4bdff4beSrobert }
67*4bdff4beSrobert 
68*4bdff4beSrobert template <class CharT>
BM_format_to_string_pointer(benchmark::State & state)69*4bdff4beSrobert static void BM_format_to_string_pointer(benchmark::State& state) {
70*4bdff4beSrobert   size_t size = state.range(0);
71*4bdff4beSrobert   auto str = std::basic_string<CharT>(size, CharT('*'));
72*4bdff4beSrobert 
73*4bdff4beSrobert   auto buffer = std::basic_string<CharT>(size, CharT('-'));
74*4bdff4beSrobert   CharT* output = buffer.data();
75*4bdff4beSrobert   for (auto _ : state)
76*4bdff4beSrobert     benchmark::DoNotOptimize(std::format_to(output, CSTR("{}"), str));
77*4bdff4beSrobert 
78*4bdff4beSrobert   state.SetBytesProcessed(state.iterations() * size * sizeof(CharT));
79*4bdff4beSrobert }
80*4bdff4beSrobert 
81*4bdff4beSrobert /*** Main ***/
82*4bdff4beSrobert 
83*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_back_inserter, std::string)->RangeMultiplier(2)->Range(1, 1 << 20);
84*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_back_inserter, std::vector<char>)->RangeMultiplier(2)->Range(1, 1 << 20);
85*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_back_inserter, std::list<char>)->RangeMultiplier(2)->Range(1, 1 << 20);
86*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_begin, std::string)->RangeMultiplier(2)->Range(1, 1 << 20);
87*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_begin, std::vector<char>)->RangeMultiplier(2)->Range(1, 1 << 20);
88*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_begin, std::list<char>)->RangeMultiplier(2)->Range(1, 1 << 20);
89*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_span, char)->RangeMultiplier(2)->Range(1, 1 << 20);
90*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_pointer, char)->RangeMultiplier(2)->Range(1, 1 << 20);
91*4bdff4beSrobert 
92*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_back_inserter, std::wstring)->RangeMultiplier(2)->Range(1, 1 << 20);
93*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_back_inserter, std::vector<wchar_t>)->RangeMultiplier(2)->Range(1, 1 << 20);
94*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_back_inserter, std::list<wchar_t>)->RangeMultiplier(2)->Range(1, 1 << 20);
95*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_begin, std::wstring)->RangeMultiplier(2)->Range(1, 1 << 20);
96*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_begin, std::vector<wchar_t>)->RangeMultiplier(2)->Range(1, 1 << 20);
97*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_begin, std::list<wchar_t>)->RangeMultiplier(2)->Range(1, 1 << 20);
98*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_span, wchar_t)->RangeMultiplier(2)->Range(1, 1 << 20);
99*4bdff4beSrobert BENCHMARK_TEMPLATE(BM_format_to_string_pointer, wchar_t)->RangeMultiplier(2)->Range(1, 1 << 20);
100*4bdff4beSrobert 
main(int argc,char ** argv)101*4bdff4beSrobert int main(int argc, char** argv) {
102*4bdff4beSrobert   benchmark::Initialize(&argc, argv);
103*4bdff4beSrobert   if (benchmark::ReportUnrecognizedArguments(argc, argv))
104*4bdff4beSrobert     return 1;
105*4bdff4beSrobert 
106*4bdff4beSrobert   benchmark::RunSpecifiedBenchmarks();
107*4bdff4beSrobert }
108