blob: 0a851e7b4537f380252be822242e1560cc9e7610 [file] [log] [blame]
Abseil Team59ae4d52018-05-18 08:24:54 -07001// Copyright 2018 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
nik727338b70432019-03-08 10:27:53 -05007// https://www.apache.org/licenses/LICENSE-2.0
Abseil Team59ae4d52018-05-18 08:24:54 -07008//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Abseil Team6d21df72024-01-05 11:13:25 -080015#include <array>
Abseil Team59ae4d52018-05-18 08:24:54 -070016#include <cstdint>
Dmitri Gribenko0ddbfd52023-08-08 09:46:31 -070017#include <cstdio>
18#include <cstdlib>
19#include <cstring>
Abseil Team59ae4d52018-05-18 08:24:54 -070020#include <string>
Abseil Team6d21df72024-01-05 11:13:25 -080021#include <tuple>
22#include <utility>
Abseil Team59ae4d52018-05-18 08:24:54 -070023
24#include "benchmark/benchmark.h"
Abseil Team6d21df72024-01-05 11:13:25 -080025#include "absl/random/log_uniform_int_distribution.h"
26#include "absl/random/random.h"
27#include "absl/strings/str_cat.h"
Dmitri Gribenko0ddbfd52023-08-08 09:46:31 -070028#include "absl/strings/string_view.h"
Abseil Team59ae4d52018-05-18 08:24:54 -070029#include "absl/strings/substitute.h"
30
31namespace {
32
33const char kStringOne[] = "Once Upon A Time, ";
Abseil Teama877af12020-03-10 09:28:06 -070034const char kStringTwo[] = "There was a string benchmark";
Abseil Team59ae4d52018-05-18 08:24:54 -070035
36// We want to include negative numbers in the benchmark, so this function
37// is used to count 0, 1, -1, 2, -2, 3, -3, ...
38inline int IncrementAlternatingSign(int i) {
39 return i > 0 ? -i : 1 - i;
40}
41
42void BM_Sum_By_StrCat(benchmark::State& state) {
43 int i = 0;
44 char foo[100];
45 for (auto _ : state) {
46 // NOLINTNEXTLINE(runtime/printf)
47 strcpy(foo, absl::StrCat(kStringOne, i, kStringTwo, i * 65536ULL).c_str());
48 int sum = 0;
49 for (char* f = &foo[0]; *f != 0; ++f) {
50 sum += *f;
51 }
52 benchmark::DoNotOptimize(sum);
53 i = IncrementAlternatingSign(i);
54 }
55}
56BENCHMARK(BM_Sum_By_StrCat);
57
58void BM_StrCat_By_snprintf(benchmark::State& state) {
59 int i = 0;
60 char on_stack[1000];
61 for (auto _ : state) {
62 snprintf(on_stack, sizeof(on_stack), "%s %s:%d", kStringOne, kStringTwo, i);
63 i = IncrementAlternatingSign(i);
64 }
65}
66BENCHMARK(BM_StrCat_By_snprintf);
67
68void BM_StrCat_By_Strings(benchmark::State& state) {
69 int i = 0;
70 for (auto _ : state) {
71 std::string result =
72 std::string(kStringOne) + " " + kStringTwo + ":" + absl::StrCat(i);
73 benchmark::DoNotOptimize(result);
74 i = IncrementAlternatingSign(i);
75 }
76}
77BENCHMARK(BM_StrCat_By_Strings);
78
79void BM_StrCat_By_StringOpPlus(benchmark::State& state) {
80 int i = 0;
81 for (auto _ : state) {
82 std::string result = kStringOne;
83 result += " ";
84 result += kStringTwo;
85 result += ":";
86 result += absl::StrCat(i);
87 benchmark::DoNotOptimize(result);
88 i = IncrementAlternatingSign(i);
89 }
90}
91BENCHMARK(BM_StrCat_By_StringOpPlus);
92
93void BM_StrCat_By_StrCat(benchmark::State& state) {
94 int i = 0;
95 for (auto _ : state) {
96 std::string result = absl::StrCat(kStringOne, " ", kStringTwo, ":", i);
97 benchmark::DoNotOptimize(result);
98 i = IncrementAlternatingSign(i);
99 }
100}
101BENCHMARK(BM_StrCat_By_StrCat);
102
103void BM_HexCat_By_StrCat(benchmark::State& state) {
104 int i = 0;
105 for (auto _ : state) {
106 std::string result =
107 absl::StrCat(kStringOne, " ", absl::Hex(int64_t{i} + 0x10000000));
108 benchmark::DoNotOptimize(result);
109 i = IncrementAlternatingSign(i);
110 }
111}
112BENCHMARK(BM_HexCat_By_StrCat);
113
114void BM_HexCat_By_Substitute(benchmark::State& state) {
115 int i = 0;
116 for (auto _ : state) {
117 std::string result = absl::Substitute(
118 "$0 $1", kStringOne, reinterpret_cast<void*>(int64_t{i} + 0x10000000));
119 benchmark::DoNotOptimize(result);
120 i = IncrementAlternatingSign(i);
121 }
122}
123BENCHMARK(BM_HexCat_By_Substitute);
124
125void BM_FloatToString_By_StrCat(benchmark::State& state) {
126 int i = 0;
127 float foo = 0.0f;
128 for (auto _ : state) {
129 std::string result = absl::StrCat(foo += 1.001f, " != ", int64_t{i});
130 benchmark::DoNotOptimize(result);
131 i = IncrementAlternatingSign(i);
132 }
133}
134BENCHMARK(BM_FloatToString_By_StrCat);
135
136void BM_DoubleToString_By_SixDigits(benchmark::State& state) {
137 int i = 0;
138 double foo = 0.0;
139 for (auto _ : state) {
140 std::string result =
141 absl::StrCat(absl::SixDigits(foo += 1.001), " != ", int64_t{i});
142 benchmark::DoNotOptimize(result);
143 i = IncrementAlternatingSign(i);
144 }
145}
146BENCHMARK(BM_DoubleToString_By_SixDigits);
147
Abseil Team6d21df72024-01-05 11:13:25 -0800148template <typename Table, size_t... Index>
149void BM_StrAppendImpl(benchmark::State& state, Table table, size_t total_bytes,
150 std::index_sequence<Index...>) {
Abseil Teamdc969f32020-08-17 15:25:15 -0700151 for (auto s : state) {
Abseil Team6d21df72024-01-05 11:13:25 -0800152 const size_t table_size = table.size();
153 size_t i = 0;
Abseil Teamdc969f32020-08-17 15:25:15 -0700154 std::string result;
155 while (result.size() < total_bytes) {
Abseil Team6d21df72024-01-05 11:13:25 -0800156 absl::StrAppend(&result, std::get<Index>(table[i])...);
Abseil Teamdc969f32020-08-17 15:25:15 -0700157 benchmark::DoNotOptimize(result);
Abseil Team6d21df72024-01-05 11:13:25 -0800158 ++i;
159 i -= i >= table_size ? table_size : 0;
Abseil Teamdc969f32020-08-17 15:25:15 -0700160 }
161 }
162}
163
Abseil Team6d21df72024-01-05 11:13:25 -0800164template <typename Array>
165void BM_StrAppend(benchmark::State& state, Array&& table) {
Abseil Teamc0466922023-11-13 11:40:08 -0800166 const size_t total_bytes = state.range(0);
Abseil Teamdc969f32020-08-17 15:25:15 -0700167 const int chunks_at_a_time = state.range(1);
Abseil Teamdc969f32020-08-17 15:25:15 -0700168
169 switch (chunks_at_a_time) {
170 case 1:
Abseil Team6d21df72024-01-05 11:13:25 -0800171 return BM_StrAppendImpl(state, std::forward<Array>(table), total_bytes,
172 std::make_index_sequence<1>());
Abseil Teamdc969f32020-08-17 15:25:15 -0700173 case 2:
Abseil Team6d21df72024-01-05 11:13:25 -0800174 return BM_StrAppendImpl(state, std::forward<Array>(table), total_bytes,
175 std::make_index_sequence<2>());
Abseil Teamdc969f32020-08-17 15:25:15 -0700176 case 4:
Abseil Team6d21df72024-01-05 11:13:25 -0800177 return BM_StrAppendImpl(state, std::forward<Array>(table), total_bytes,
178 std::make_index_sequence<4>());
Abseil Teamdc969f32020-08-17 15:25:15 -0700179 case 8:
Abseil Team6d21df72024-01-05 11:13:25 -0800180 return BM_StrAppendImpl(state, std::forward<Array>(table), total_bytes,
181 std::make_index_sequence<8>());
Abseil Teamdc969f32020-08-17 15:25:15 -0700182 default:
183 std::abort();
184 }
185}
186
Abseil Team6d21df72024-01-05 11:13:25 -0800187void BM_StrAppendStr(benchmark::State& state) {
188 using T = absl::string_view;
189 using Row = std::tuple<T, T, T, T, T, T, T, T>;
190 constexpr absl::string_view kChunk = "0123456789";
191 Row row = {kChunk, kChunk, kChunk, kChunk, kChunk, kChunk, kChunk, kChunk};
192 return BM_StrAppend(state, std::array<Row, 1>({row}));
193}
Abseil Teamc0466922023-11-13 11:40:08 -0800194
Abseil Team6d21df72024-01-05 11:13:25 -0800195template <typename T>
196void BM_StrAppendInt(benchmark::State& state) {
197 absl::BitGen rng;
198 absl::log_uniform_int_distribution<T> dist;
199 std::array<std::tuple<T, T, T, T, T, T, T, T>, (1 << 7)> table;
200 for (size_t i = 0; i < table.size(); ++i) {
201 table[i] = {dist(rng), dist(rng), dist(rng), dist(rng),
202 dist(rng), dist(rng), dist(rng), dist(rng)};
Abseil Teamc0466922023-11-13 11:40:08 -0800203 }
Abseil Team6d21df72024-01-05 11:13:25 -0800204 return BM_StrAppend(state, table);
Abseil Teamc0466922023-11-13 11:40:08 -0800205}
206
Abseil Teamdc969f32020-08-17 15:25:15 -0700207template <typename B>
208void StrAppendConfig(B* benchmark) {
209 for (int bytes : {10, 100, 1000, 10000}) {
210 for (int chunks : {1, 2, 4, 8}) {
211 // Only add the ones that divide properly. Otherwise we are over counting.
212 if (bytes % (10 * chunks) == 0) {
213 benchmark->Args({bytes, chunks});
214 }
215 }
216 }
217}
218
Abseil Team6d21df72024-01-05 11:13:25 -0800219BENCHMARK(BM_StrAppendStr)->Apply(StrAppendConfig);
220BENCHMARK(BM_StrAppendInt<int64_t>)->Apply(StrAppendConfig);
221BENCHMARK(BM_StrAppendInt<uint64_t>)->Apply(StrAppendConfig);
222BENCHMARK(BM_StrAppendInt<int32_t>)->Apply(StrAppendConfig);
223BENCHMARK(BM_StrAppendInt<uint32_t>)->Apply(StrAppendConfig);
Abseil Teamdc969f32020-08-17 15:25:15 -0700224
Abseil Teambd467aa2023-09-19 09:07:16 -0700225template <typename... Chunks>
226void BM_StrCatImpl(benchmark::State& state,
227 Chunks... chunks) {
228 for (auto s : state) {
229 std::string result = absl::StrCat(chunks...);
230 benchmark::DoNotOptimize(result);
231 }
232}
233
234void BM_StrCat(benchmark::State& state) {
235 const int chunks_at_a_time = state.range(0);
236 const absl::string_view kChunk = "0123456789";
237
238 switch (chunks_at_a_time) {
239 case 1:
240 return BM_StrCatImpl(state, kChunk);
241 case 2:
242 return BM_StrCatImpl(state, kChunk, kChunk);
243 case 3:
244 return BM_StrCatImpl(state, kChunk, kChunk, kChunk);
245 case 4:
246 return BM_StrCatImpl(state, kChunk, kChunk, kChunk, kChunk);
247 default:
248 std::abort();
249 }
250}
251
252BENCHMARK(BM_StrCat)->Arg(1)->Arg(2)->Arg(3)->Arg(4);
253
Abseil Team65d7b6d2023-08-17 07:04:14 -0700254void BM_StrCat_int(benchmark::State& state) {
255 int i = 0;
256 for (auto s : state) {
257 std::string result = absl::StrCat(i);
258 benchmark::DoNotOptimize(result);
259 i = IncrementAlternatingSign(i);
260 }
261}
262
263BENCHMARK(BM_StrCat_int);
264
Abseil Team59ae4d52018-05-18 08:24:54 -0700265} // namespace