xref: /aosp_15_r20/external/abseil-cpp/absl/strings/str_join_benchmark.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 //
2 // Copyright 2018 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "absl/strings/str_join.h"
17 
18 #include <string>
19 #include <tuple>
20 #include <vector>
21 #include <utility>
22 
23 #include "benchmark/benchmark.h"
24 
25 namespace {
26 
BM_Join2_Strings(benchmark::State & state)27 void BM_Join2_Strings(benchmark::State& state) {
28   const int string_len = state.range(0);
29   const int num_strings = state.range(1);
30   const std::string s(string_len, 'x');
31   const std::vector<std::string> v(num_strings, s);
32   for (auto _ : state) {
33     std::string s = absl::StrJoin(v, "-");
34     benchmark::DoNotOptimize(s);
35   }
36 }
37 BENCHMARK(BM_Join2_Strings)
38     ->ArgPair(1 << 0, 1 << 3)
39     ->ArgPair(1 << 10, 1 << 3)
40     ->ArgPair(1 << 13, 1 << 3)
41     ->ArgPair(1 << 0, 1 << 10)
42     ->ArgPair(1 << 10, 1 << 10)
43     ->ArgPair(1 << 13, 1 << 10)
44     ->ArgPair(1 << 0, 1 << 13)
45     ->ArgPair(1 << 10, 1 << 13)
46     ->ArgPair(1 << 13, 1 << 13);
47 
BM_Join2_Ints(benchmark::State & state)48 void BM_Join2_Ints(benchmark::State& state) {
49   const int num_ints = state.range(0);
50   const std::vector<int> v(num_ints, 42);
51   for (auto _ : state) {
52     std::string s = absl::StrJoin(v, "-");
53     benchmark::DoNotOptimize(s);
54   }
55 }
56 BENCHMARK(BM_Join2_Ints)->Range(0, 1 << 13);
57 
BM_Join2_KeysAndValues(benchmark::State & state)58 void BM_Join2_KeysAndValues(benchmark::State& state) {
59   const int string_len = state.range(0);
60   const int num_pairs = state.range(1);
61   const std::string s(string_len, 'x');
62   const std::vector<std::pair<std::string, int>> v(num_pairs,
63                                                    std::make_pair(s, 42));
64   for (auto _ : state) {
65     std::string s = absl::StrJoin(v, ",", absl::PairFormatter("="));
66     benchmark::DoNotOptimize(s);
67   }
68 }
69 BENCHMARK(BM_Join2_KeysAndValues)
70     ->ArgPair(1 << 0, 1 << 3)
71     ->ArgPair(1 << 10, 1 << 3)
72     ->ArgPair(1 << 13, 1 << 3)
73     ->ArgPair(1 << 0, 1 << 10)
74     ->ArgPair(1 << 10, 1 << 10)
75     ->ArgPair(1 << 13, 1 << 10)
76     ->ArgPair(1 << 0, 1 << 13)
77     ->ArgPair(1 << 10, 1 << 13)
78     ->ArgPair(1 << 13, 1 << 13);
79 
BM_JoinStreamable(benchmark::State & state)80 void BM_JoinStreamable(benchmark::State& state) {
81   const int string_len = state.range(0);
82   const int num_strings = state.range(1);
83   const std::vector<std::string> v(num_strings, std::string(string_len, 'x'));
84   for (auto _ : state) {
85     std::string s = absl::StrJoin(v, "", absl::StreamFormatter());
86     benchmark::DoNotOptimize(s);
87   }
88 }
89 BENCHMARK(BM_JoinStreamable)
90     ->ArgPair(0, 0)
91     ->ArgPair(16, 1)
92     ->ArgPair(256, 1)
93     ->ArgPair(16, 16)
94     ->ArgPair(256, 16)
95     ->ArgPair(16, 256)
96     ->ArgPair(256, 256);
97 
BM_JoinTuple(benchmark::State & state)98 void BM_JoinTuple(benchmark::State& state) {
99   for (auto _ : state) {
100     std::string s =
101         absl::StrJoin(std::make_tuple(123456789, 987654321, 24680, 13579), "/");
102     benchmark::DoNotOptimize(s);
103   }
104 }
105 BENCHMARK(BM_JoinTuple);
106 
107 }  // namespace
108