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