proxygen
StringBenchmark.cpp File Reference
#include <folly/String.h>
#include <boost/algorithm/string.hpp>
#include <folly/Benchmark.h>
#include <folly/Random.h>
#include <random>

Go to the source code of this file.

Functions

 BENCHMARK (libc_tolower, iters)
 
 BENCHMARK (folly_toLowerAscii, iters)
 
void stringPrintfOutputSize (int iters, int param)
 
 BENCHMARK (stringPrintfAppendfBenchmark, iters)
 
 BENCHMARK (splitOnSingleChar, iters)
 
 BENCHMARK (splitOnSingleCharFixed, iters)
 
 BENCHMARK (splitOnSingleCharFixedAllowExtra, iters)
 
 BENCHMARK (splitStr, iters)
 
 BENCHMARK (splitStrFixed, iters)
 
 BENCHMARK (boost_splitOnSingleChar, iters)
 
 BENCHMARK (joinCharStr, iters)
 
 BENCHMARK (joinStrStr, iters)
 
 BENCHMARK (joinInt, iters)
 
int main (int argc, char **argv)
 

Function Documentation

BENCHMARK ( libc_tolower  ,
iters   
)

Definition at line 27 of file StringBenchmark.cpp.

References i.

27  {
28  static const size_t kSize = 256;
29  // This array is static to keep the compiler from optimizing the
30  // entire function down to a no-op if it has an inlined impl of
31  // tolower and thus is able to tell that there are no side-effects.
32  // No side-effects + no writes to anything other than local variables
33  // + no return value = no need to run any of the code in the function.
34  // gcc, for example, makes that optimization with -O2.
35  static char input[kSize];
36  for (size_t i = 0; i < kSize; i++) {
37  input[i] = (char)(i & 0xff);
38  }
39  for (auto i = iters; i > 0; i--) {
40  for (size_t offset = 0; offset < kSize; offset++) {
41  input[offset] = tolower(input[offset]);
42  }
43  }
44 }
BENCHMARK ( folly_toLowerAscii  ,
iters   
)

Definition at line 46 of file StringBenchmark.cpp.

References i, and folly::toLowerAscii().

46  {
47  static const size_t kSize = 256;
48  static char input[kSize];
49  for (size_t i = 0; i < kSize; i++) {
50  input[i] = (char)(i & 0xff);
51  }
52  for (auto i = iters; i > 0; i--) {
53  folly::toLowerAscii(input, kSize);
54  }
55 }
void toLowerAscii(char *str, size_t length)
Definition: String.cpp:601
BENCHMARK ( stringPrintfAppendfBenchmark  ,
iters   
)

Definition at line 82 of file StringBenchmark.cpp.

References folly::BENCHMARK(), BENCHMARK_SUSPEND, c, folly::doNotOptimizeAway(), encode(), folly::hexlify(), i, folly::gen::detail::passthrough(), folly::basic_fbstring< E, T, A, Storage >::push_back(), folly::basic_fbstring< E, T, A, Storage >::reserve(), folly::basic_fbstring< E, T, A, Storage >::resize(), s, folly::Random::secureRandom(), folly::Range< Iter >::size(), folly::basic_fbstring< E, T, A, Storage >::size(), string, folly::stringAppendf(), folly::Range< Iter >::subpiece(), uint32_t, and folly::unhexlify().

82  {
83  for (unsigned int i = 0; i < iters; ++i) {
84  string s;
86  s.reserve(300000);
87  }
88  for (int j = 0; j < 300000; ++j) {
89  stringAppendf(&s, "%d", 1);
90  }
91  }
92 }
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
std::string & stringAppendf(std::string *output, const char *format,...)
Definition: String.cpp:240
static set< string > s
BENCHMARK ( splitOnSingleChar  ,
iters   
)

Definition at line 207 of file StringBenchmark.cpp.

References i, folly::split(), and string.

207  {
208  static const std::string line = "one:two:three:four";
209  for (size_t i = 0; i < iters << 4; ++i) {
210  std::vector<StringPiece> pieces;
211  folly::split(':', line, pieces);
212  }
213 }
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
const char * string
Definition: Conv.cpp:212
BENCHMARK ( splitOnSingleCharFixed  ,
iters   
)

Definition at line 215 of file StringBenchmark.cpp.

References a, b, c, i, folly::split(), and string.

215  {
216  static const std::string line = "one:two:three:four";
217  for (size_t i = 0; i < iters << 4; ++i) {
218  StringPiece a, b, c, d;
219  folly::split(':', line, a, b, c, d);
220  }
221 }
char b
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
char a
const char * string
Definition: Conv.cpp:212
char c
BENCHMARK ( splitOnSingleCharFixedAllowExtra  ,
iters   
)

Definition at line 223 of file StringBenchmark.cpp.

References a, b, c, i, and string.

223  {
224  static const std::string line = "one:two:three:four";
225  for (size_t i = 0; i < iters << 4; ++i) {
226  StringPiece a, b, c, d;
227  folly::split<false>(':', line, a, b, c, d);
228  }
229 }
char b
char a
const char * string
Definition: Conv.cpp:212
char c
BENCHMARK ( splitStr  ,
iters   
)

Definition at line 231 of file StringBenchmark.cpp.

References i, folly::split(), and string.

231  {
232  static const std::string line = "one-*-two-*-three-*-four";
233  for (size_t i = 0; i < iters << 4; ++i) {
234  std::vector<StringPiece> pieces;
235  folly::split("-*-", line, pieces);
236  }
237 }
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
const char * string
Definition: Conv.cpp:212
BENCHMARK ( splitStrFixed  ,
iters   
)

Definition at line 239 of file StringBenchmark.cpp.

References a, b, c, i, folly::split(), and string.

239  {
240  static const std::string line = "one-*-two-*-three-*-four";
241  for (size_t i = 0; i < iters << 4; ++i) {
242  StringPiece a, b, c, d;
243  folly::split("-*-", line, a, b, c, d);
244  }
245 }
char b
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
char a
const char * string
Definition: Conv.cpp:212
char c
BENCHMARK ( boost_splitOnSingleChar  ,
iters   
)

Definition at line 247 of file StringBenchmark.cpp.

References c, i, folly::gen::split(), and string.

247  {
248  static const std::string line = "one:two:three:four";
249  bool (*pred)(char) = [](char c) -> bool { return c == ':'; };
250  for (size_t i = 0; i < iters << 4; ++i) {
251  std::vector<boost::iterator_range<std::string::const_iterator>> pieces;
252  boost::split(pieces, line, pred);
253  }
254 }
S split(const StringPiece source, char delimiter)
Definition: String.h:61
const char * string
Definition: Conv.cpp:212
char c
BENCHMARK ( joinCharStr  ,
iters   
)

Definition at line 256 of file StringBenchmark.cpp.

References i, folly::join(), gmock_output_test::output, and string.

256  {
257  static const std::vector<std::string> input = {
258  "one", "two", "three", "four", "five", "six", "seven"};
259  for (size_t i = 0; i < iters << 4; ++i) {
261  folly::join(':', input, output);
262  }
263 }
const char * string
Definition: Conv.cpp:212
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
BENCHMARK ( joinStrStr  ,
iters   
)

Definition at line 265 of file StringBenchmark.cpp.

References i, folly::join(), gmock_output_test::output, and string.

265  {
266  static const std::vector<std::string> input = {
267  "one", "two", "three", "four", "five", "six", "seven"};
268  for (size_t i = 0; i < iters << 4; ++i) {
270  folly::join(":", input, output);
271  }
272 }
const char * string
Definition: Conv.cpp:212
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
BENCHMARK ( joinInt  ,
iters   
)

Definition at line 274 of file StringBenchmark.cpp.

References i, folly::join(), gmock_output_test::output, and string.

274  {
275  static const auto input = {123, 456, 78910, 1112, 1314, 151, 61718};
276  for (size_t i = 0; i < iters << 4; ++i) {
278  folly::join(":", input, output);
279  }
280 }
const char * string
Definition: Conv.cpp:212
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
int main ( int  argc,
char **  argv 
)

Definition at line 282 of file StringBenchmark.cpp.

References folly::runBenchmarks().

282  {
283  gflags::ParseCommandLineFlags(&argc, &argv, true);
284  initBenchmark();
286  return 0;
287 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void stringPrintfOutputSize ( int  iters,
int  param 
)

Definition at line 59 of file StringBenchmark.cpp.

References BENCHMARK_PARAM, BENCHMARK_SUSPEND, buffer(), i, int64_t, s, and folly::stringPrintf().

59  {
60  string buffer;
62  buffer.resize(param, 'x');
63  }
64 
65  for (int64_t i = 0; i < iters; ++i) {
66  string s = stringPrintf("msg: %d, %d, %s", 10, 20, buffer.c_str());
67  }
68 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
std::string stringPrintf(const char *format,...)
Definition: String.cpp:223
static set< string > s