str_split_benchmark.cc
Go to the documentation of this file.
1 // 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"
26 
27 namespace {
28 
29 std::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 
38 void 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 }
45 BENCHMARK_RANGE(BM_Split2StringView, 0, 1 << 20);
46 
47 void BM_Split2StringViewLifted(benchmark::State& state) {
48  std::string test = MakeTestString(state.range(0));
49  std::vector<absl::string_view> result;
50  for (auto _ : state) {
51  result = absl::StrSplit(test, ';');
52  }
53  benchmark::DoNotOptimize(result);
54 }
55 BENCHMARK_RANGE(BM_Split2StringViewLifted, 0, 1 << 20);
56 
57 void BM_Split2String(benchmark::State& state) {
58  std::string test = MakeTestString(state.range(0));
59  for (auto _ : state) {
60  std::vector<std::string> result = absl::StrSplit(test, ';');
61  benchmark::DoNotOptimize(result);
62  }
63 }
64 BENCHMARK_RANGE(BM_Split2String, 0, 1 << 20);
65 
66 // This benchmark is for comparing Split2 to Split1 (SplitStringUsing). In
67 // particular, this benchmark uses SkipEmpty() to match SplitStringUsing's
68 // behavior.
69 void BM_Split2SplitStringUsing(benchmark::State& state) {
70  std::string test = MakeTestString(state.range(0));
71  for (auto _ : state) {
72  std::vector<std::string> result =
73  absl::StrSplit(test, ';', absl::SkipEmpty());
74  benchmark::DoNotOptimize(result);
75  }
76 }
77 BENCHMARK_RANGE(BM_Split2SplitStringUsing, 0, 1 << 20);
78 
79 void BM_SplitStringToUnorderedSet(benchmark::State& state) {
80  const int len = state.range(0);
81  std::string test(len, 'x');
82  for (int i = 1; i < len; i += 2) {
83  test[i] = ';';
84  }
85  for (auto _ : state) {
86  std::unordered_set<std::string> result =
87  absl::StrSplit(test, ':', absl::SkipEmpty());
88  benchmark::DoNotOptimize(result);
89  }
90 }
91 BENCHMARK_RANGE(BM_SplitStringToUnorderedSet, 0, 1 << 20);
92 
93 void BM_SplitStringToUnorderedMap(benchmark::State& state) {
94  const int len = state.range(0);
95  std::string test(len, 'x');
96  for (int i = 1; i < len; i += 2) {
97  test[i] = ';';
98  }
99  for (auto _ : state) {
100  std::unordered_map<std::string, std::string> result =
101  absl::StrSplit(test, ':', absl::SkipEmpty());
102  benchmark::DoNotOptimize(result);
103  }
104 }
105 BENCHMARK_RANGE(BM_SplitStringToUnorderedMap, 0, 1 << 20);
106 
107 void BM_SplitStringAllowEmpty(benchmark::State& state) {
108  const int len = state.range(0);
109  std::string test(len, 'x');
110  for (int i = 1; i < len; i += 2) {
111  test[i] = ';';
112  }
113  for (auto _ : state) {
114  std::vector<std::string> result = absl::StrSplit(test, ';');
115  benchmark::DoNotOptimize(result);
116  }
117 }
118 BENCHMARK_RANGE(BM_SplitStringAllowEmpty, 0, 1 << 20);
119 
120 struct OneCharLiteral {
121  char operator()() const { return 'X'; }
122 };
123 
124 struct OneCharStringLiteral {
125  const char* operator()() const { return "X"; }
126 };
127 
128 template <typename DelimiterFactory>
129 void BM_SplitStringWithOneChar(benchmark::State& state) {
130  const auto delimiter = DelimiterFactory()();
131  std::vector<absl::string_view> pieces;
132  size_t v = 0;
133  for (auto _ : state) {
134  pieces = absl::StrSplit("The quick brown fox jumps over the lazy dog",
135  delimiter);
136  v += pieces.size();
137  }
138  ABSL_RAW_CHECK(v == state.iterations(), "");
139 }
140 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharLiteral);
141 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharStringLiteral);
142 
143 template <typename DelimiterFactory>
144 void BM_SplitStringWithOneCharNoVector(benchmark::State& state) {
145  const auto delimiter = DelimiterFactory()();
146  size_t v = 0;
147  for (auto _ : state) {
148  auto splitter = absl::StrSplit(
149  "The quick brown fox jumps over the lazy dog", delimiter);
150  v += std::distance(splitter.begin(), splitter.end());
151  }
152  ABSL_RAW_CHECK(v == state.iterations(), "");
153 }
154 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharLiteral);
155 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharStringLiteral);
156 
157 } // namespace
int v
Definition: variant_test.cc:81
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
Definition: str_split.h:491
#define ABSL_RAW_CHECK(condition, message)
Definition: raw_logging.h:57


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58