19 #include <unordered_map> 20 #include <unordered_set> 23 #include "benchmark/benchmark.h" 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) {
38 void BM_Split2StringView(benchmark::State& state) {
39 std::string test = MakeTestString(state.range(0));
40 for (
auto _ : state) {
42 benchmark::DoNotOptimize(result);
45 BENCHMARK_RANGE(BM_Split2StringView, 0, 1 << 20);
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) {
53 benchmark::DoNotOptimize(result);
55 BENCHMARK_RANGE(BM_Split2StringViewLifted, 0, 1 << 20);
57 void BM_Split2String(benchmark::State& state) {
58 std::string test = MakeTestString(state.range(0));
59 for (
auto _ : state) {
61 benchmark::DoNotOptimize(result);
64 BENCHMARK_RANGE(BM_Split2String, 0, 1 << 20);
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 =
74 benchmark::DoNotOptimize(result);
77 BENCHMARK_RANGE(BM_Split2SplitStringUsing, 0, 1 << 20);
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) {
85 for (
auto _ : state) {
86 std::unordered_set<std::string> result =
88 benchmark::DoNotOptimize(result);
91 BENCHMARK_RANGE(BM_SplitStringToUnorderedSet, 0, 1 << 20);
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) {
99 for (
auto _ : state) {
100 std::unordered_map<std::string, std::string> result =
102 benchmark::DoNotOptimize(result);
105 BENCHMARK_RANGE(BM_SplitStringToUnorderedMap, 0, 1 << 20);
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) {
113 for (
auto _ : state) {
115 benchmark::DoNotOptimize(result);
118 BENCHMARK_RANGE(BM_SplitStringAllowEmpty, 0, 1 << 20);
120 struct OneCharLiteral {
121 char operator()()
const {
return 'X'; }
124 struct OneCharStringLiteral {
125 const char* operator()()
const {
return "X"; }
128 template <
typename DelimiterFactory>
129 void BM_SplitStringWithOneChar(benchmark::State& state) {
130 const auto delimiter = DelimiterFactory()();
131 std::vector<absl::string_view> pieces;
133 for (
auto _ : state) {
134 pieces =
absl::StrSplit(
"The quick brown fox jumps over the lazy dog",
140 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharLiteral);
141 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharStringLiteral);
143 template <
typename DelimiterFactory>
144 void BM_SplitStringWithOneCharNoVector(benchmark::State& state) {
145 const auto delimiter = DelimiterFactory()();
147 for (
auto _ : state) {
149 "The quick brown fox jumps over the lazy dog", delimiter);
150 v += std::distance(splitter.begin(), splitter.end());
154 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharLiteral);
155 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharStringLiteral);
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
#define ABSL_RAW_CHECK(condition, message)