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 <vector>
20 #include <utility>
21
22 #include "benchmark/benchmark.h"
23
24 namespace {
25
BM_Join2_Strings(benchmark::State & state)26 void BM_Join2_Strings(benchmark::State& state) {
27 const int string_len = state.range(0);
28 const int num_strings = state.range(1);
29 const std::string s(string_len, 'x');
30 const std::vector<std::string> v(num_strings, s);
31 for (auto _ : state) {
32 std::string s = absl::StrJoin(v, "-");
33 benchmark::DoNotOptimize(s);
34 }
35 }
36 BENCHMARK(BM_Join2_Strings)
37 ->ArgPair(1 << 0, 1 << 3)
38 ->ArgPair(1 << 10, 1 << 3)
39 ->ArgPair(1 << 13, 1 << 3)
40 ->ArgPair(1 << 0, 1 << 10)
41 ->ArgPair(1 << 10, 1 << 10)
42 ->ArgPair(1 << 13, 1 << 10)
43 ->ArgPair(1 << 0, 1 << 13)
44 ->ArgPair(1 << 10, 1 << 13)
45 ->ArgPair(1 << 13, 1 << 13);
46
BM_Join2_Ints(benchmark::State & state)47 void BM_Join2_Ints(benchmark::State& state) {
48 const int num_ints = state.range(0);
49 const std::vector<int> v(num_ints, 42);
50 for (auto _ : state) {
51 std::string s = absl::StrJoin(v, "-");
52 benchmark::DoNotOptimize(s);
53 }
54 }
55 BENCHMARK(BM_Join2_Ints)->Range(0, 1 << 13);
56
BM_Join2_KeysAndValues(benchmark::State & state)57 void BM_Join2_KeysAndValues(benchmark::State& state) {
58 const int string_len = state.range(0);
59 const int num_pairs = state.range(1);
60 const std::string s(string_len, 'x');
61 const std::vector<std::pair<std::string, int>> v(num_pairs,
62 std::make_pair(s, 42));
63 for (auto _ : state) {
64 std::string s = absl::StrJoin(v, ",", absl::PairFormatter("="));
65 benchmark::DoNotOptimize(s);
66 }
67 }
68 BENCHMARK(BM_Join2_KeysAndValues)
69 ->ArgPair(1 << 0, 1 << 3)
70 ->ArgPair(1 << 10, 1 << 3)
71 ->ArgPair(1 << 13, 1 << 3)
72 ->ArgPair(1 << 0, 1 << 10)
73 ->ArgPair(1 << 10, 1 << 10)
74 ->ArgPair(1 << 13, 1 << 10)
75 ->ArgPair(1 << 0, 1 << 13)
76 ->ArgPair(1 << 10, 1 << 13)
77 ->ArgPair(1 << 13, 1 << 13);
78
BM_JoinStreamable(benchmark::State & state)79 void BM_JoinStreamable(benchmark::State& state) {
80 const int string_len = state.range(0);
81 const int num_strings = state.range(1);
82 const std::vector<std::string> v(num_strings, std::string(string_len, 'x'));
83 for (auto _ : state) {
84 std::string s = absl::StrJoin(v, "", absl::StreamFormatter());
85 benchmark::DoNotOptimize(s);
86 }
87 }
88 BENCHMARK(BM_JoinStreamable)
89 ->ArgPair(0, 0)
90 ->ArgPair(16, 1)
91 ->ArgPair(256, 1)
92 ->ArgPair(16, 16)
93 ->ArgPair(256, 16)
94 ->ArgPair(16, 256)
95 ->ArgPair(256, 256);
96
97 } // namespace
98