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 <string>
11
12#include "benchmark/benchmark.h"
13#include "make_string.h"
14
15#define CSTR(S) MAKE_CSTRING(CharT, S)
16
17template <class CharT>
18static void BM_format_string(benchmark::State& state) {
19 size_t size = state.range(pos: 0);
20 std::basic_string<CharT> str(size, CharT('*'));
21
22 while (state.KeepRunningBatch(str.size()))
23 benchmark::DoNotOptimize(std::format(CSTR("{}"), str));
24
25 state.SetBytesProcessed(state.iterations() * size * sizeof(CharT));
26}
27BENCHMARK_TEMPLATE(BM_format_string, char)->RangeMultiplier(multiplier: 2)->Range(start: 1, limit: 1 << 20);
28BENCHMARK_TEMPLATE(BM_format_string, wchar_t)->RangeMultiplier(multiplier: 2)->Range(start: 1, limit: 1 << 20);
29
30int main(int argc, char** argv) {
31 benchmark::Initialize(argc: &argc, argv);
32 if (benchmark::ReportUnrecognizedArguments(argc, argv))
33 return 1;
34
35 benchmark::RunSpecifiedBenchmarks();
36}
37

source code of libcxx/benchmarks/format.bench.cpp