blob: f38dfcfe5af7ff999af9188cc8f3a39a65a6b70f [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -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//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
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
15#include "absl/strings/str_split.h"
16
17#include <iterator>
18#include <string>
19#include <unordered_map>
20#include <unordered_set>
21#include <vector>
22
23#include "benchmark/benchmark.h"
24#include "absl/base/internal/raw_logging.h"
25#include "absl/strings/string_view.h"
26
27namespace {
28
29std::string MakeTestString(int desired_length) {
30 static const int kAverageValueLen = 25;
31 std::string test(desired_length * kAverageValueLen, 'x');
32 for (int i = 1; i < test.size(); i += kAverageValueLen) {
33 test[i] = ';';
34 }
35 return test;
36}
37
38void BM_Split2StringView(benchmark::State& state) {
39 std::string test = MakeTestString(state.range(0));
40 for (auto _ : state) {
41 std::vector<absl::string_view> result = absl::StrSplit(test, ';');
42 benchmark::DoNotOptimize(result);
43 }
44}
45BENCHMARK_RANGE(BM_Split2StringView, 0, 1 << 20);
46
47static const absl::string_view kDelimiters = ";:,.";
48
49std::string MakeMultiDelimiterTestString(int desired_length) {
50 static const int kAverageValueLen = 25;
51 std::string test(desired_length * kAverageValueLen, 'x');
52 for (int i = 0; i * kAverageValueLen < test.size(); ++i) {
53 // Cycle through a variety of delimiters.
54 test[i * kAverageValueLen] = kDelimiters[i % kDelimiters.size()];
55 }
56 return test;
57}
58
59// Measure StrSplit with ByAnyChar with four delimiters to choose from.
60void BM_Split2StringViewByAnyChar(benchmark::State& state) {
61 std::string test = MakeMultiDelimiterTestString(state.range(0));
62 for (auto _ : state) {
63 std::vector<absl::string_view> result =
64 absl::StrSplit(test, absl::ByAnyChar(kDelimiters));
65 benchmark::DoNotOptimize(result);
66 }
67}
68BENCHMARK_RANGE(BM_Split2StringViewByAnyChar, 0, 1 << 20);
69
70void BM_Split2StringViewLifted(benchmark::State& state) {
71 std::string test = MakeTestString(state.range(0));
72 std::vector<absl::string_view> result;
73 for (auto _ : state) {
74 result = absl::StrSplit(test, ';');
75 }
76 benchmark::DoNotOptimize(result);
77}
78BENCHMARK_RANGE(BM_Split2StringViewLifted, 0, 1 << 20);
79
80void BM_Split2String(benchmark::State& state) {
81 std::string test = MakeTestString(state.range(0));
82 for (auto _ : state) {
83 std::vector<std::string> result = absl::StrSplit(test, ';');
84 benchmark::DoNotOptimize(result);
85 }
86}
87BENCHMARK_RANGE(BM_Split2String, 0, 1 << 20);
88
89// This benchmark is for comparing Split2 to Split1 (SplitStringUsing). In
90// particular, this benchmark uses SkipEmpty() to match SplitStringUsing's
91// behavior.
92void BM_Split2SplitStringUsing(benchmark::State& state) {
93 std::string test = MakeTestString(state.range(0));
94 for (auto _ : state) {
95 std::vector<std::string> result =
96 absl::StrSplit(test, ';', absl::SkipEmpty());
97 benchmark::DoNotOptimize(result);
98 }
99}
100BENCHMARK_RANGE(BM_Split2SplitStringUsing, 0, 1 << 20);
101
102void BM_SplitStringToUnorderedSet(benchmark::State& state) {
103 const int len = state.range(0);
104 std::string test(len, 'x');
105 for (int i = 1; i < len; i += 2) {
106 test[i] = ';';
107 }
108 for (auto _ : state) {
109 std::unordered_set<std::string> result =
110 absl::StrSplit(test, ':', absl::SkipEmpty());
111 benchmark::DoNotOptimize(result);
112 }
113}
114BENCHMARK_RANGE(BM_SplitStringToUnorderedSet, 0, 1 << 20);
115
116void BM_SplitStringToUnorderedMap(benchmark::State& state) {
117 const int len = state.range(0);
118 std::string test(len, 'x');
119 for (int i = 1; i < len; i += 2) {
120 test[i] = ';';
121 }
122 for (auto _ : state) {
123 std::unordered_map<std::string, std::string> result =
124 absl::StrSplit(test, ':', absl::SkipEmpty());
125 benchmark::DoNotOptimize(result);
126 }
127}
128BENCHMARK_RANGE(BM_SplitStringToUnorderedMap, 0, 1 << 20);
129
130void BM_SplitStringAllowEmpty(benchmark::State& state) {
131 const int len = state.range(0);
132 std::string test(len, 'x');
133 for (int i = 1; i < len; i += 2) {
134 test[i] = ';';
135 }
136 for (auto _ : state) {
137 std::vector<std::string> result = absl::StrSplit(test, ';');
138 benchmark::DoNotOptimize(result);
139 }
140}
141BENCHMARK_RANGE(BM_SplitStringAllowEmpty, 0, 1 << 20);
142
143struct OneCharLiteral {
144 char operator()() const { return 'X'; }
145};
146
147struct OneCharStringLiteral {
148 const char* operator()() const { return "X"; }
149};
150
151template <typename DelimiterFactory>
152void BM_SplitStringWithOneChar(benchmark::State& state) {
153 const auto delimiter = DelimiterFactory()();
154 std::vector<absl::string_view> pieces;
155 size_t v = 0;
156 for (auto _ : state) {
157 pieces = absl::StrSplit("The quick brown fox jumps over the lazy dog",
158 delimiter);
159 v += pieces.size();
160 }
161 ABSL_RAW_CHECK(v == state.iterations(), "");
162}
163BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharLiteral);
164BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharStringLiteral);
165
166template <typename DelimiterFactory>
167void BM_SplitStringWithOneCharNoVector(benchmark::State& state) {
168 const auto delimiter = DelimiterFactory()();
169 size_t v = 0;
170 for (auto _ : state) {
171 auto splitter = absl::StrSplit(
172 "The quick brown fox jumps over the lazy dog", delimiter);
173 v += std::distance(splitter.begin(), splitter.end());
174 }
175 ABSL_RAW_CHECK(v == state.iterations(), "");
176}
177BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharLiteral);
178BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharStringLiteral);
179
180} // namespace