proxygen
FormatBenchmark.cpp File Reference
#include <folly/Format.h>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/FBVector.h>
#include <folly/Utility.h>
#include <folly/dynamic.h>
#include <folly/init/Init.h>
#include <folly/json.h>

Go to the source code of this file.

Functions

 BENCHMARK (octal_snprintf, iters)
 
 BENCHMARK_RELATIVE (octal_uintToOctal, iters)
 
 BENCHMARK_DRAW_LINE ()
 
 BENCHMARK (hex_snprintf, iters)
 
 BENCHMARK_RELATIVE (hex_uintToHex, iters)
 
 BENCHMARK (intAppend_snprintf)
 
 BENCHMARK_RELATIVE (intAppend_to)
 
 BENCHMARK_RELATIVE (intAppend_format)
 
template<size_t... Indexes>
int snprintf20Numbers (int i, index_sequence< Indexes... >)
 
 BENCHMARK (bigFormat_snprintf, iters)
 
template<size_t... Indexes>
decltype(auto) format20Numbers (int i, index_sequence< Indexes... >)
 
 BENCHMARK_RELATIVE (bigFormat_format, iters)
 
 BENCHMARK (format_nested_strings, iters)
 
 BENCHMARK_RELATIVE (format_nested_fbstrings, iters)
 
 BENCHMARK_RELATIVE (format_nested_direct, iters)
 
 BENCHMARK (copy_short_string, iters)
 
 BENCHMARK_RELATIVE (format_short_string_unsafe, iters)
 
 BENCHMARK_RELATIVE (format_short_string_safe, iters)
 
 BENCHMARK_RELATIVE (sformat_short_string_unsafe, iters)
 
 BENCHMARK_RELATIVE (sformat_short_string_safe, iters)
 
 BENCHMARK (copy_long_string, iters)
 
 BENCHMARK_RELATIVE (format_long_string_unsafe, iters)
 
 BENCHMARK_RELATIVE (format_long_string_safe, iters)
 
 BENCHMARK_RELATIVE (sformat_long_string_unsafe, iters)
 
 BENCHMARK_RELATIVE (sformat_long_string_safe, iters)
 
int main (int argc, char *argv[])
 

Function Documentation

BENCHMARK ( octal_snprintf  ,
iters   
)

Definition at line 44 of file FormatBenchmark.cpp.

44  {
45  while (iters--) {
46  snprintf(
47  bigBuf.data(), bigBuf.size(), "%o", static_cast<unsigned int>(iters));
48  }
49 }
BENCHMARK ( hex_snprintf  ,
iters   
)

Definition at line 62 of file FormatBenchmark.cpp.

62  {
63  while (iters--) {
64  snprintf(
65  bigBuf.data(), bigBuf.size(), "%x", static_cast<unsigned int>(iters));
66  }
67 }
BENCHMARK ( intAppend_snprintf  )

Definition at line 78 of file FormatBenchmark.cpp.

References folly::basic_fbstring< E, T, A, Storage >::append(), and i.

78  {
79  fbstring out;
80  for (int i = -1000; i < 1000; i++) {
81  snprintf(bigBuf.data(), bigBuf.size(), "%d", i);
82  out.append(bigBuf.data());
83  }
84 }
basic_fbstring & append(const basic_fbstring &str)
Definition: FBString.h:1953
BENCHMARK ( bigFormat_snprintf  ,
iters   
)

Definition at line 115 of file FormatBenchmark.cpp.

References i, and snprintf20Numbers().

115  {
116  while (iters--) {
117  for (int i = -100; i < 100; i++) {
119  }
120  }
121 }
int snprintf20Numbers(int i, index_sequence< Indexes... >)
make_integer_sequence< std::size_t, Size > make_index_sequence
Definition: Utility.h:209
BENCHMARK ( format_nested_strings  ,
iters   
)

Definition at line 153 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::format(), and i.

153  {
154  BenchmarkSuspender suspender;
155  while (iters--) {
156  for (int i = 0; i < 1000; ++i) {
157  fbstring out;
158  suspender.dismissing([&] {
159  format(
160  &out,
161  "{} {}",
162  format("{} {}", i, i + 1).str(),
163  format("{} {}", -i, -i - 1).str());
164  });
165  }
166  }
167 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK ( copy_short_string  ,
iters   
)

Definition at line 203 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing().

203  {
204  BenchmarkSuspender suspender;
205  auto const& shortString = getShortString();
206  while (iters--) {
207  fbstring out;
208  suspender.dismissing([&] { out = shortString; });
209  }
210 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
BENCHMARK ( copy_long_string  ,
iters   
)

Definition at line 250 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing().

250  {
251  BenchmarkSuspender suspender;
252  auto const& longString = getLongString();
253  while (iters--) {
254  fbstring out;
255  suspender.dismissing([&] { out = longString; });
256  }
257 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
BENCHMARK_DRAW_LINE ( )
BENCHMARK_RELATIVE ( octal_uintToOctal  ,
iters   
)

Definition at line 51 of file FormatBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::detail::kMaxOctalLength, and folly::detail::uintToOctal().

51  {
52  while (iters--) {
54  bigBuf.data(),
56  static_cast<unsigned int>(iters));
57  }
58 }
size_t uintToOctal(char *buffer, size_t bufLen, Uint v)
Definition: Format-inl.h:112
const size_t kMaxOctalLength
Definition: Format-inl.h:52
BENCHMARK_RELATIVE ( hex_uintToHex  ,
iters   
)

Definition at line 69 of file FormatBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::detail::kMaxHexLength, and folly::detail::uintToHexLower().

69  {
70  while (iters--) {
72  bigBuf.data(), detail::kMaxHexLength, static_cast<unsigned int>(iters));
73  }
74 }
size_t uintToHexLower(char *buffer, size_t bufLen, Uint v)
Definition: Format-inl.h:90
const size_t kMaxHexLength
Definition: Format-inl.h:51
BENCHMARK_RELATIVE ( intAppend_to  )

Definition at line 86 of file FormatBenchmark.cpp.

References i, and folly::toAppend().

86  {
87  fbstring out;
88  for (int i = -1000; i < 1000; i++) {
89  toAppend(i, &out);
90  }
91 }
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
BENCHMARK_RELATIVE ( intAppend_format  )

Definition at line 93 of file FormatBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::format(), and i.

93  {
94  fbstring out;
95  for (int i = -1000; i < 1000; i++) {
96  format(&out, "{}", i);
97  }
98 }
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( bigFormat_format  ,
iters   
)

Definition at line 134 of file FormatBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::BenchmarkSuspender::dismissing(), format20Numbers(), and i.

134  {
135  BenchmarkSuspender suspender;
136  char* p;
137  auto writeToBuf = [&p](StringPiece sp) mutable {
138  memcpy(p, sp.data(), sp.size());
139  p += sp.size();
140  };
141 
142  while (iters--) {
143  for (int i = -100; i < 100; i++) {
144  p = bigBuf.data();
145  suspender.dismissing(
146  [&] { format20Numbers(i, make_index_sequence<20>())(writeToBuf); });
147  }
148  }
149 }
decltype(auto) format20Numbers(int i, index_sequence< Indexes... >)
make_integer_sequence< std::size_t, Size > make_index_sequence
Definition: Utility.h:209
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
BENCHMARK_RELATIVE ( format_nested_fbstrings  ,
iters   
)

Definition at line 169 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::format(), and i.

169  {
170  BenchmarkSuspender suspender;
171  while (iters--) {
172  for (int i = 0; i < 1000; ++i) {
173  fbstring out;
174  suspender.dismissing([&] {
175  format(
176  &out,
177  "{} {}",
178  format("{} {}", i, i + 1).fbstr(),
179  format("{} {}", -i, -i - 1).fbstr());
180  });
181  }
182  }
183 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( format_nested_direct  ,
iters   
)

Definition at line 185 of file FormatBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::BenchmarkSuspender::dismissing(), folly::format(), and i.

185  {
186  BenchmarkSuspender suspender;
187  while (iters--) {
188  for (int i = 0; i < 1000; ++i) {
189  fbstring out;
190  suspender.dismissing([&] {
191  format(
192  &out,
193  "{} {}",
194  format("{} {}", i, i + 1),
195  format("{} {}", -i, -i - 1));
196  });
197  }
198  }
199 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( format_short_string_unsafe  ,
iters   
)

Definition at line 212 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), and folly::format().

212  {
213  BenchmarkSuspender suspender;
214  auto const& shortString = getShortString();
215  while (iters--) {
216  fbstring out;
217  suspender.dismissing([&] { format(&out, shortString); });
218  }
219 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( format_short_string_safe  ,
iters   
)

Definition at line 221 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), and folly::format().

221  {
222  BenchmarkSuspender suspender;
223  auto const& shortString = getShortString();
224  while (iters--) {
225  fbstring out;
226  suspender.dismissing([&] { format(&out, "{}", shortString); });
227  }
228 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( sformat_short_string_unsafe  ,
iters   
)

Definition at line 230 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::sformat(), and string.

230  {
231  BenchmarkSuspender suspender;
232  auto const& shortString = getShortString();
233  while (iters--) {
234  std::string out;
235  suspender.dismissing([&] { out = sformat(shortString); });
236  }
237 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
BENCHMARK_RELATIVE ( sformat_short_string_safe  ,
iters   
)

Definition at line 239 of file FormatBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::BenchmarkSuspender::dismissing(), folly::sformat(), and string.

239  {
240  BenchmarkSuspender suspender;
241  auto const& shortString = getShortString();
242  while (iters--) {
243  std::string out;
244  suspender.dismissing([&] { out = sformat("{}", shortString); });
245  }
246 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
BENCHMARK_RELATIVE ( format_long_string_unsafe  ,
iters   
)

Definition at line 259 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), and folly::format().

259  {
260  BenchmarkSuspender suspender;
261  auto const& longString = getLongString();
262  while (iters--) {
263  fbstring out;
264  suspender.dismissing([&] { format(&out, longString); });
265  }
266 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( format_long_string_safe  ,
iters   
)

Definition at line 268 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), and folly::format().

268  {
269  BenchmarkSuspender suspender;
270  auto const& longString = getLongString();
271  while (iters--) {
272  fbstring out;
273  suspender.dismissing([&] { format(&out, "{}", longString); });
274  }
275 }
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
BENCHMARK_RELATIVE ( sformat_long_string_unsafe  ,
iters   
)

Definition at line 277 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::sformat(), and string.

277  {
278  BenchmarkSuspender suspender;
279  auto const& longString = getLongString();
280  while (iters--) {
281  std::string out;
282  suspender.dismissing([&] { out = sformat(longString); });
283  }
284 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
BENCHMARK_RELATIVE ( sformat_long_string_safe  ,
iters   
)

Definition at line 286 of file FormatBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::sformat(), and string.

286  {
287  BenchmarkSuspender suspender;
288  auto const& longString = getLongString();
289  while (iters--) {
290  std::string out;
291  suspender.dismissing([&] { out = sformat("{}", longString); });
292  }
293 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
template<size_t... Indexes>
decltype(auto) format20Numbers ( int  i,
index_sequence< Indexes... >   
)

Definition at line 124 of file FormatBenchmark.cpp.

References folly::format().

Referenced by BENCHMARK_RELATIVE().

124  {
125  static_assert(20 == sizeof...(Indexes), "Must have exactly 20 indexes");
126  return format(
127  "{} {} {} {} {}"
128  "{} {} {} {} {}"
129  "{} {} {} {} {}"
130  "{} {} {} {} {}",
131  (i + static_cast<int>(Indexes))...);
132 }
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
int main ( int  argc,
char *  argv[] 
)

Definition at line 330 of file FormatBenchmark.cpp.

References folly::init(), and folly::runBenchmarks().

330  {
331  init(&argc, &argv, true);
332  runBenchmarks();
333  return 0;
334 }
void runBenchmarks()
Definition: Benchmark.cpp:456
void init()
char ** argv
template<size_t... Indexes>
int snprintf20Numbers ( int  i,
index_sequence< Indexes... >   
)

Definition at line 103 of file FormatBenchmark.cpp.

Referenced by BENCHMARK().

103  {
104  static_assert(20 == sizeof...(Indexes), "Must have exactly 20 indexes");
105  return snprintf(
106  bigBuf.data(),
107  bigBuf.size(),
108  "%d %d %d %d %d"
109  "%d %d %d %d %d"
110  "%d %d %d %d %d"
111  "%d %d %d %d %d",
112  (i + static_cast<int>(Indexes))...);
113 }