proxygen
StringBenchmark.cpp File Reference
#include <atomic>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/String.h>
#include <folly/container/Foreach.h>
#include <folly/gen/Base.h>
#include <folly/gen/String.h>

Go to the source code of this file.

Functions

 BENCHMARK (StringResplitter_Big, iters)
 
 BENCHMARK_RELATIVE (StringResplitter_Small, iters)
 
 BENCHMARK_DRAW_LINE ()
 
 BENCHMARK (StringSplit_Old, iters)
 
 BENCHMARK_RELATIVE (StringSplit_Gen_Vector, iters)
 
 BENCHMARK (StringSplit_Old_ReuseVector, iters)
 
 BENCHMARK_RELATIVE (StringSplit_Gen_ReuseVector, iters)
 
 BENCHMARK_RELATIVE (StringSplit_Gen, iters)
 
 BENCHMARK_RELATIVE (StringSplit_Gen_Take, iters)
 
 BENCHMARK (StringUnsplit_Old, iters)
 
 BENCHMARK_RELATIVE (StringUnsplit_Old_ReusedBuffer, iters)
 
 BENCHMARK_RELATIVE (StringUnsplit_Gen, iters)
 
 BENCHMARK_RELATIVE (StringUnsplit_Gen_ReusedBuffer, iters)
 
void StringUnsplit_Gen (size_t iters, size_t joinSize)
 
void Lines_Gen (size_t iters, int joinSize)
 
 BENCHMARK (Records_EachToTuple, iters)
 
 BENCHMARK_RELATIVE (Records_VectorStringPieceReused, iters)
 
 BENCHMARK_RELATIVE (Records_VectorStringPiece, iters)
 
 BENCHMARK_RELATIVE (Records_VectorString, iters)
 
int main (int argc, char *argv[])
 

Variables

fbstring records
 

Function Documentation

BENCHMARK ( StringResplitter_Big  ,
iters   
)

Definition at line 68 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::map(), folly::gen::resplit(), s, and folly::gen::sum.

68  {
69  size_t s = 0;
70  while (iters--) {
71  s += from({bigLines}) | resplit('\n') | map(&len) | sum;
72  }
74 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
std::atomic< int64_t > sum(0)
static Map map(mapCap)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( StringSplit_Old  ,
iters   
)

Definition at line 86 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), s, folly::gen::split(), and string.

86  {
87  size_t s = 0;
88  std::string line(kLine);
89  while (iters--) {
90  std::vector<StringPiece> parts;
91  split(' ', line, parts);
92  s += parts.size();
93  }
95 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
const char * string
Definition: Conv.cpp:212
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( StringSplit_Old_ReuseVector  ,
iters   
)

Definition at line 108 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), s, folly::gen::split(), and string.

108  {
109  size_t s = 0;
110  std::string line(kLine);
111  std::vector<StringPiece> parts;
112  while (iters--) {
113  parts.clear();
114  split(' ', line, parts);
115  s += parts.size();
116  }
118 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
const char * string
Definition: Conv.cpp:212
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( StringUnsplit_Old  ,
iters   
)

Definition at line 152 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::join(), s, and folly::basic_fbstring< E, T, A, Storage >::size().

152  {
153  size_t s = 0;
154  while (iters--) {
155  fbstring joined;
156  join(',', testStrVector, joined);
157  s += joined.size();
158  }
160 }
size_type size() const
Definition: FBString.h:1337
#define join
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Records_EachToTuple  ,
iters   
)

Definition at line 239 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), i, s, folly::gen::split(), and folly::gen::sum.

239  {
240  size_t s = 0;
241  for (size_t i = 0; i < iters; i += 1000) {
242  // clang-format off
243  s += split(records, '\n')
244  | eachToTuple<int, size_t, StringPiece>(' ')
245  | get<1>()
246  | sum;
247  // clang-format on
248  }
250 }
std::atomic< int64_t > sum(0)
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static set< string > s
fbstring records
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_DRAW_LINE ( )
BENCHMARK_RELATIVE ( StringResplitter_Small  ,
iters   
)

Definition at line 76 of file StringBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::map(), folly::gen::resplit(), s, and folly::gen::sum.

76  {
77  size_t s = 0;
78  while (iters--) {
79  s += from(smallLines) | resplit('\n') | map(&len) | sum;
80  }
82 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
std::atomic< int64_t > sum(0)
static Map map(mapCap)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringSplit_Gen_Vector  ,
iters   
)

Definition at line 97 of file StringBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), s, and folly::gen::split().

97  {
98  size_t s = 0;
99  StringPiece line(kLine);
100  while (iters--) {
101  s += (split(line, ' ') | as<vector>()).size();
102  }
104 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringSplit_Gen_ReuseVector  ,
iters   
)

Definition at line 120 of file StringBenchmark.cpp.

References folly::gen::appendTo(), folly::doNotOptimizeAway(), s, and folly::gen::split().

120  {
121  size_t s = 0;
122  StringPiece line(kLine);
123  std::vector<StringPiece> parts;
124  while (iters--) {
125  parts.clear();
126  split(line, ' ') | appendTo(parts);
127  s += parts.size();
128  }
130 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
Append appendTo(Collection &collection)
Definition: Base.h:824
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringSplit_Gen  ,
iters   
)

Definition at line 132 of file StringBenchmark.cpp.

References folly::gen::count, folly::doNotOptimizeAway(), s, and folly::gen::split().

132  {
133  size_t s = 0;
134  StringPiece line(kLine);
135  while (iters--) {
136  s += split(line, ' ') | count;
137  }
139 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
int * count
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringSplit_Gen_Take  ,
iters   
)

Definition at line 141 of file StringBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::gen::count, folly::doNotOptimizeAway(), s, folly::gen::split(), and folly::gen::take().

141  {
142  size_t s = 0;
143  StringPiece line(kLine);
144  while (iters--) {
145  s += split(line, ' ') | take(10) | count;
146  }
148 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
int * count
static set< string > s
detail::Take take(Number count)
Definition: Base-inl.h:2582
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringUnsplit_Old_ReusedBuffer  ,
iters   
)

Definition at line 162 of file StringBenchmark.cpp.

References folly::basic_fbstring< E, T, A, Storage >::clear(), folly::doNotOptimizeAway(), folly::join(), s, and folly::basic_fbstring< E, T, A, Storage >::size().

162  {
163  size_t s = 0;
164  fbstring joined;
165  while (iters--) {
166  joined.clear();
167  join(',', testStrVector, joined);
168  s += joined.size();
169  }
171 }
size_type size() const
Definition: FBString.h:1337
#define join
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringUnsplit_Gen  ,
iters   
)

Definition at line 173 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::from(), s, folly::basic_fbstring< E, T, A, Storage >::size(), and folly::gen::unsplit().

173  {
174  size_t s = 0;
175  while (iters--) {
176  fbstring joined = from(testStrVector) | unsplit(',');
177  s += joined.size();
178  }
180 }
size_type size() const
Definition: FBString.h:1337
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
Unsplit unsplit(const Delimiter &delimiter)
Definition: String.h:101
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( StringUnsplit_Gen_ReusedBuffer  ,
iters   
)

Definition at line 182 of file StringBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), buffer(), folly::basic_fbstring< E, T, A, Storage >::clear(), folly::doNotOptimizeAway(), folly::gen::from(), s, folly::basic_fbstring< E, T, A, Storage >::size(), and folly::gen::unsplit().

182  {
183  size_t s = 0;
185  while (iters--) {
186  buffer.clear();
187  from(testStrVector) | unsplit(',', &buffer);
188  s += buffer.size();
189  }
191 }
size_type size() const
Definition: FBString.h:1337
std::vector< uint8_t > buffer(kBufferSize+16)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
Unsplit unsplit(const Delimiter &delimiter)
Definition: String.h:101
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Records_VectorStringPieceReused  ,
iters   
)

Definition at line 252 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), i, std::tr1::make_tuple(), folly::gen::mapped(), s, folly::gen::split(), folly::split(), and folly::gen::sum.

252  {
253  size_t s = 0;
254  std::vector<StringPiece> fields;
255  for (size_t i = 0; i < iters; i += 1000) {
256  // clang-format off
257  s += split(records, '\n')
258  | mapped([&](StringPiece line) {
259  fields.clear();
260  folly::split(' ', line, fields);
261  CHECK(fields.size() == 3);
262  return std::make_tuple(
263  folly::to<int>(fields[0]),
264  folly::to<size_t>(fields[1]),
265  StringPiece(fields[2]));
266  })
267  | get<1>()
268  | sum;
269  // clang-format on
270  }
272 }
std::atomic< int64_t > sum(0)
tuple make_tuple()
Definition: gtest-tuple.h:675
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static set< string > s
Map mapped(Predicate pred=Predicate())
Definition: Base.h:540
Range< const char * > StringPiece
fbstring records
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Records_VectorStringPiece  ,
iters   
)

Definition at line 274 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), i, std::tr1::make_tuple(), folly::gen::mapped(), s, folly::gen::split(), folly::split(), and folly::gen::sum.

274  {
275  size_t s = 0;
276  for (size_t i = 0; i < iters; i += 1000) {
277  // clang-format off
278  s += split(records, '\n')
279  | mapped([](StringPiece line) {
280  std::vector<StringPiece> fields;
281  folly::split(' ', line, fields);
282  CHECK(fields.size() == 3);
283  return std::make_tuple(
284  folly::to<int>(fields[0]),
285  folly::to<size_t>(fields[1]),
286  StringPiece(fields[2]));
287  })
288  | get<1>()
289  | sum;
290  // clang-format on
291  }
293 }
std::atomic< int64_t > sum(0)
tuple make_tuple()
Definition: gtest-tuple.h:675
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static set< string > s
Map mapped(Predicate pred=Predicate())
Definition: Base.h:540
Range< const char * > StringPiece
fbstring records
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Records_VectorString  ,
iters   
)

Definition at line 295 of file StringBenchmark.cpp.

References folly::doNotOptimizeAway(), i, std::tr1::make_tuple(), folly::gen::mapped(), s, folly::gen::split(), folly::split(), and folly::gen::sum.

295  {
296  size_t s = 0;
297  for (size_t i = 0; i < iters; i += 1000) {
298  // clang-format off
299  s += split(records, '\n')
300  | mapped([](StringPiece line) {
301  std::vector<std::string> fields;
302  folly::split(' ', line, fields);
303  CHECK(fields.size() == 3);
304  return std::make_tuple(
305  folly::to<int>(fields[0]),
306  folly::to<size_t>(fields[1]),
307  StringPiece(fields[2]));
308  })
309  | get<1>()
310  | sum;
311  // clang-format on
312  }
314 }
std::atomic< int64_t > sum(0)
tuple make_tuple()
Definition: gtest-tuple.h:675
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
S split(const StringPiece source, char delimiter)
Definition: String.h:61
static set< string > s
Map mapped(Predicate pred=Predicate())
Definition: Base.h:540
Range< const char * > StringPiece
fbstring records
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
void Lines_Gen ( size_t  iters,
int  joinSize 
)

Definition at line 216 of file StringBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), BENCHMARK_PARAM, BENCHMARK_RELATIVE_PARAM, folly::gen::count, folly::doNotOptimizeAway(), i, folly::gen::lines(), s, folly::Range< Iter >::subpiece(), and folly::gen::take().

216  {
217  size_t s = 0;
218  StringPiece content = testFileContent;
219  for (size_t i = 0; i < iters; ++i) {
220  s += lines(content.subpiece(0, joinSize)) | take(100) | count;
221  }
223 }
Range subpiece(size_type first, size_type length=npos) const
Definition: Range.h:686
S lines(StringPiece source)
Definition: String.h:80
int * count
static set< string > s
detail::Take take(Number count)
Definition: Base-inl.h:2582
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
int main ( int  argc,
char *  argv[] 
)

Definition at line 347 of file StringBenchmark.cpp.

References folly::runBenchmarks().

347  {
348  gflags::ParseCommandLineFlags(&argc, &argv, true);
349  initStringResplitterBenchmark();
350  runBenchmarks();
351  return 0;
352 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void StringUnsplit_Gen ( size_t  iters,
size_t  joinSize 
)

Definition at line 195 of file StringBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), BENCHMARK_PARAM, BENCHMARK_RELATIVE_PARAM, BENCHMARK_SUSPEND, buffer(), folly::basic_fbstring< E, T, A, Storage >::clear(), folly::doNotOptimizeAway(), FOR_EACH_RANGE, folly::gen::from(), i, s, folly::basic_fbstring< E, T, A, Storage >::size(), folly::gen::unsplit(), and v.

195  {
196  std::vector<fbstring> v;
198  FOR_EACH_RANGE (i, 0, joinSize) { v.push_back(to<fbstring>(rand())); }
199  }
200  size_t s = 0;
202  while (iters--) {
203  buffer.clear();
204  from(v) | unsplit(',', &buffer);
205  s += buffer.size();
206  }
208 }
size_type size() const
Definition: FBString.h:1337
std::vector< uint8_t > buffer(kBufferSize+16)
auto v
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
Unsplit unsplit(const Delimiter &delimiter)
Definition: String.h:101
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258

Variable Documentation

fbstring records
Initial value:
= seq<size_t>(1, 1000)
| mapped([](size_t i) {
return folly::to<fbstring>(i, ' ', i * i, ' ', i * i * i);
})
| unsplit('\n')
Map mapped(Predicate pred=Predicate())
Definition: Base.h:540
Unsplit unsplit(const Delimiter &delimiter)
Definition: String.h:101

Definition at line 232 of file StringBenchmark.cpp.

Referenced by folly::test::TEST().