Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/str_split.h"
00016
00017 #include <iterator>
00018 #include <string>
00019 #include <unordered_map>
00020 #include <unordered_set>
00021 #include <vector>
00022
00023 #include "benchmark/benchmark.h"
00024 #include "absl/base/internal/raw_logging.h"
00025 #include "absl/strings/string_view.h"
00026
00027 namespace {
00028
00029 std::string MakeTestString(int desired_length) {
00030 static const int kAverageValueLen = 25;
00031 std::string test(desired_length * kAverageValueLen, 'x');
00032 for (int i = 1; i < test.size(); i += kAverageValueLen) {
00033 test[i] = ';';
00034 }
00035 return test;
00036 }
00037
00038 void BM_Split2StringView(benchmark::State& state) {
00039 std::string test = MakeTestString(state.range(0));
00040 for (auto _ : state) {
00041 std::vector<absl::string_view> result = absl::StrSplit(test, ';');
00042 benchmark::DoNotOptimize(result);
00043 }
00044 }
00045 BENCHMARK_RANGE(BM_Split2StringView, 0, 1 << 20);
00046
00047 void BM_Split2StringViewLifted(benchmark::State& state) {
00048 std::string test = MakeTestString(state.range(0));
00049 std::vector<absl::string_view> result;
00050 for (auto _ : state) {
00051 result = absl::StrSplit(test, ';');
00052 }
00053 benchmark::DoNotOptimize(result);
00054 }
00055 BENCHMARK_RANGE(BM_Split2StringViewLifted, 0, 1 << 20);
00056
00057 void BM_Split2String(benchmark::State& state) {
00058 std::string test = MakeTestString(state.range(0));
00059 for (auto _ : state) {
00060 std::vector<std::string> result = absl::StrSplit(test, ';');
00061 benchmark::DoNotOptimize(result);
00062 }
00063 }
00064 BENCHMARK_RANGE(BM_Split2String, 0, 1 << 20);
00065
00066
00067
00068
00069 void BM_Split2SplitStringUsing(benchmark::State& state) {
00070 std::string test = MakeTestString(state.range(0));
00071 for (auto _ : state) {
00072 std::vector<std::string> result =
00073 absl::StrSplit(test, ';', absl::SkipEmpty());
00074 benchmark::DoNotOptimize(result);
00075 }
00076 }
00077 BENCHMARK_RANGE(BM_Split2SplitStringUsing, 0, 1 << 20);
00078
00079 void BM_SplitStringToUnorderedSet(benchmark::State& state) {
00080 const int len = state.range(0);
00081 std::string test(len, 'x');
00082 for (int i = 1; i < len; i += 2) {
00083 test[i] = ';';
00084 }
00085 for (auto _ : state) {
00086 std::unordered_set<std::string> result =
00087 absl::StrSplit(test, ':', absl::SkipEmpty());
00088 benchmark::DoNotOptimize(result);
00089 }
00090 }
00091 BENCHMARK_RANGE(BM_SplitStringToUnorderedSet, 0, 1 << 20);
00092
00093 void BM_SplitStringToUnorderedMap(benchmark::State& state) {
00094 const int len = state.range(0);
00095 std::string test(len, 'x');
00096 for (int i = 1; i < len; i += 2) {
00097 test[i] = ';';
00098 }
00099 for (auto _ : state) {
00100 std::unordered_map<std::string, std::string> result =
00101 absl::StrSplit(test, ':', absl::SkipEmpty());
00102 benchmark::DoNotOptimize(result);
00103 }
00104 }
00105 BENCHMARK_RANGE(BM_SplitStringToUnorderedMap, 0, 1 << 20);
00106
00107 void BM_SplitStringAllowEmpty(benchmark::State& state) {
00108 const int len = state.range(0);
00109 std::string test(len, 'x');
00110 for (int i = 1; i < len; i += 2) {
00111 test[i] = ';';
00112 }
00113 for (auto _ : state) {
00114 std::vector<std::string> result = absl::StrSplit(test, ';');
00115 benchmark::DoNotOptimize(result);
00116 }
00117 }
00118 BENCHMARK_RANGE(BM_SplitStringAllowEmpty, 0, 1 << 20);
00119
00120 struct OneCharLiteral {
00121 char operator()() const { return 'X'; }
00122 };
00123
00124 struct OneCharStringLiteral {
00125 const char* operator()() const { return "X"; }
00126 };
00127
00128 template <typename DelimiterFactory>
00129 void BM_SplitStringWithOneChar(benchmark::State& state) {
00130 const auto delimiter = DelimiterFactory()();
00131 std::vector<absl::string_view> pieces;
00132 size_t v = 0;
00133 for (auto _ : state) {
00134 pieces = absl::StrSplit("The quick brown fox jumps over the lazy dog",
00135 delimiter);
00136 v += pieces.size();
00137 }
00138 ABSL_RAW_CHECK(v == state.iterations(), "");
00139 }
00140 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharLiteral);
00141 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharStringLiteral);
00142
00143 template <typename DelimiterFactory>
00144 void BM_SplitStringWithOneCharNoVector(benchmark::State& state) {
00145 const auto delimiter = DelimiterFactory()();
00146 size_t v = 0;
00147 for (auto _ : state) {
00148 auto splitter = absl::StrSplit(
00149 "The quick brown fox jumps over the lazy dog", delimiter);
00150 v += std::distance(splitter.begin(), splitter.end());
00151 }
00152 ABSL_RAW_CHECK(v == state.iterations(), "");
00153 }
00154 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharLiteral);
00155 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharStringLiteral);
00156
00157 }