xref: /llvm-project/llvm/benchmarks/FormatVariadicBM.cpp (revision b55c52c047a167f42abbde9a33356cfb96b82c7f)
1*9ce4af5cSRahul Joshi //===- FormatVariadicBM.cpp - formatv() benchmark ---------- --------------===//
2*9ce4af5cSRahul Joshi //
3*9ce4af5cSRahul Joshi // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*9ce4af5cSRahul Joshi // See https://llvm.org/LICENSE.txt for license information.
5*9ce4af5cSRahul Joshi // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*9ce4af5cSRahul Joshi //
7*9ce4af5cSRahul Joshi //===----------------------------------------------------------------------===//
8*9ce4af5cSRahul Joshi 
9*9ce4af5cSRahul Joshi #include "benchmark/benchmark.h"
10*9ce4af5cSRahul Joshi #include "llvm/Support/FormatVariadic.h"
11*9ce4af5cSRahul Joshi #include <algorithm>
12*9ce4af5cSRahul Joshi #include <string>
13*9ce4af5cSRahul Joshi #include <vector>
14*9ce4af5cSRahul Joshi 
15*9ce4af5cSRahul Joshi using namespace llvm;
16*9ce4af5cSRahul Joshi using namespace std;
17*9ce4af5cSRahul Joshi 
18*9ce4af5cSRahul Joshi // Generate a list of format strings that have `NumReplacements` replacements
19*9ce4af5cSRahul Joshi // by permuting the replacements and some literal text.
20*9ce4af5cSRahul Joshi static vector<string> getFormatStrings(int NumReplacements) {
21*9ce4af5cSRahul Joshi   vector<string> Components;
22*9ce4af5cSRahul Joshi   for (int I = 0; I < NumReplacements; I++)
23*9ce4af5cSRahul Joshi     Components.push_back("{" + to_string(I) + "}");
24*9ce4af5cSRahul Joshi   // Intersperse these with some other literal text (_).
25*9ce4af5cSRahul Joshi   const string_view Literal = "____";
26*9ce4af5cSRahul Joshi   for (char C : Literal)
27*9ce4af5cSRahul Joshi     Components.push_back(string(1, C));
28*9ce4af5cSRahul Joshi 
29*9ce4af5cSRahul Joshi   vector<string> Formats;
30*9ce4af5cSRahul Joshi   do {
31*9ce4af5cSRahul Joshi     string Concat;
32*9ce4af5cSRahul Joshi     for (const string &C : Components)
33*9ce4af5cSRahul Joshi       Concat += C;
34*9ce4af5cSRahul Joshi     Formats.emplace_back(Concat);
35*9ce4af5cSRahul Joshi   } while (next_permutation(Components.begin(), Components.end()));
36*9ce4af5cSRahul Joshi   return Formats;
37*9ce4af5cSRahul Joshi }
38*9ce4af5cSRahul Joshi 
39*9ce4af5cSRahul Joshi // Generate the set of formats to exercise outside the benchmark code.
40*9ce4af5cSRahul Joshi static const vector<vector<string>> Formats = {
41*9ce4af5cSRahul Joshi     getFormatStrings(1), getFormatStrings(2), getFormatStrings(3),
42*9ce4af5cSRahul Joshi     getFormatStrings(4), getFormatStrings(5),
43*9ce4af5cSRahul Joshi };
44*9ce4af5cSRahul Joshi 
45*9ce4af5cSRahul Joshi // Benchmark formatv() for a variety of format strings and 1-5 replacements.
46*9ce4af5cSRahul Joshi static void BM_FormatVariadic(benchmark::State &state) {
47*9ce4af5cSRahul Joshi   for (auto _ : state) {
48*9ce4af5cSRahul Joshi     for (const string &Fmt : Formats[0])
49*9ce4af5cSRahul Joshi       formatv(Fmt.c_str(), 1).str();
50*9ce4af5cSRahul Joshi     for (const string &Fmt : Formats[1])
51*9ce4af5cSRahul Joshi       formatv(Fmt.c_str(), 1, 2).str();
52*9ce4af5cSRahul Joshi     for (const string &Fmt : Formats[2])
53*9ce4af5cSRahul Joshi       formatv(Fmt.c_str(), 1, 2, 3).str();
54*9ce4af5cSRahul Joshi     for (const string &Fmt : Formats[3])
55*9ce4af5cSRahul Joshi       formatv(Fmt.c_str(), 1, 2, 3, 4).str();
56*9ce4af5cSRahul Joshi     for (const string &Fmt : Formats[4])
57*9ce4af5cSRahul Joshi       formatv(Fmt.c_str(), 1, 2, 3, 4, 5).str();
58*9ce4af5cSRahul Joshi   }
59*9ce4af5cSRahul Joshi }
60*9ce4af5cSRahul Joshi 
61*9ce4af5cSRahul Joshi BENCHMARK(BM_FormatVariadic);
62*9ce4af5cSRahul Joshi 
63*9ce4af5cSRahul Joshi BENCHMARK_MAIN();
64