FormatBenchmark.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Format.h>
  17. #include <glog/logging.h>
  18. #include <folly/Benchmark.h>
  19. #include <folly/FBVector.h>
  20. #include <folly/Utility.h>
  21. #include <folly/dynamic.h>
  22. #include <folly/init/Init.h>
  23. #include <folly/json.h>
  24. using namespace folly;
  25. namespace {
  26. std::array<char, 300> bigBuf;
  27. std::string getShortString() {
  28. return "ABCDEFGHIJ";
  29. }
  30. std::string getLongString() {
  31. return std::string(256, 'A');
  32. }
  33. } // namespace
  34. BENCHMARK(octal_snprintf, iters) {
  35. while (iters--) {
  36. snprintf(
  37. bigBuf.data(), bigBuf.size(), "%o", static_cast<unsigned int>(iters));
  38. }
  39. }
  40. BENCHMARK_RELATIVE(octal_uintToOctal, iters) {
  41. while (iters--) {
  42. detail::uintToOctal(
  43. bigBuf.data(),
  44. detail::kMaxOctalLength,
  45. static_cast<unsigned int>(iters));
  46. }
  47. }
  48. BENCHMARK_DRAW_LINE();
  49. BENCHMARK(hex_snprintf, iters) {
  50. while (iters--) {
  51. snprintf(
  52. bigBuf.data(), bigBuf.size(), "%x", static_cast<unsigned int>(iters));
  53. }
  54. }
  55. BENCHMARK_RELATIVE(hex_uintToHex, iters) {
  56. while (iters--) {
  57. detail::uintToHexLower(
  58. bigBuf.data(), detail::kMaxHexLength, static_cast<unsigned int>(iters));
  59. }
  60. }
  61. BENCHMARK_DRAW_LINE();
  62. BENCHMARK(intAppend_snprintf) {
  63. fbstring out;
  64. for (int i = -1000; i < 1000; i++) {
  65. snprintf(bigBuf.data(), bigBuf.size(), "%d", i);
  66. out.append(bigBuf.data());
  67. }
  68. }
  69. BENCHMARK_RELATIVE(intAppend_to) {
  70. fbstring out;
  71. for (int i = -1000; i < 1000; i++) {
  72. toAppend(i, &out);
  73. }
  74. }
  75. BENCHMARK_RELATIVE(intAppend_format) {
  76. fbstring out;
  77. for (int i = -1000; i < 1000; i++) {
  78. format(&out, "{}", i);
  79. }
  80. }
  81. BENCHMARK_DRAW_LINE();
  82. template <size_t... Indexes>
  83. int snprintf20Numbers(int i, index_sequence<Indexes...>) {
  84. static_assert(20 == sizeof...(Indexes), "Must have exactly 20 indexes");
  85. return snprintf(
  86. bigBuf.data(),
  87. bigBuf.size(),
  88. "%d %d %d %d %d"
  89. "%d %d %d %d %d"
  90. "%d %d %d %d %d"
  91. "%d %d %d %d %d",
  92. (i + static_cast<int>(Indexes))...);
  93. }
  94. BENCHMARK(bigFormat_snprintf, iters) {
  95. while (iters--) {
  96. for (int i = -100; i < 100; i++) {
  97. snprintf20Numbers(i, make_index_sequence<20>());
  98. }
  99. }
  100. }
  101. template <size_t... Indexes>
  102. decltype(auto) format20Numbers(int i, index_sequence<Indexes...>) {
  103. static_assert(20 == sizeof...(Indexes), "Must have exactly 20 indexes");
  104. return format(
  105. "{} {} {} {} {}"
  106. "{} {} {} {} {}"
  107. "{} {} {} {} {}"
  108. "{} {} {} {} {}",
  109. (i + static_cast<int>(Indexes))...);
  110. }
  111. BENCHMARK_RELATIVE(bigFormat_format, iters) {
  112. BenchmarkSuspender suspender;
  113. char* p;
  114. auto writeToBuf = [&p](StringPiece sp) mutable {
  115. memcpy(p, sp.data(), sp.size());
  116. p += sp.size();
  117. };
  118. while (iters--) {
  119. for (int i = -100; i < 100; i++) {
  120. p = bigBuf.data();
  121. suspender.dismissing(
  122. [&] { format20Numbers(i, make_index_sequence<20>())(writeToBuf); });
  123. }
  124. }
  125. }
  126. BENCHMARK_DRAW_LINE();
  127. BENCHMARK(format_nested_strings, iters) {
  128. BenchmarkSuspender suspender;
  129. while (iters--) {
  130. for (int i = 0; i < 1000; ++i) {
  131. fbstring out;
  132. suspender.dismissing([&] {
  133. format(
  134. &out,
  135. "{} {}",
  136. format("{} {}", i, i + 1).str(),
  137. format("{} {}", -i, -i - 1).str());
  138. });
  139. }
  140. }
  141. }
  142. BENCHMARK_RELATIVE(format_nested_fbstrings, iters) {
  143. BenchmarkSuspender suspender;
  144. while (iters--) {
  145. for (int i = 0; i < 1000; ++i) {
  146. fbstring out;
  147. suspender.dismissing([&] {
  148. format(
  149. &out,
  150. "{} {}",
  151. format("{} {}", i, i + 1).fbstr(),
  152. format("{} {}", -i, -i - 1).fbstr());
  153. });
  154. }
  155. }
  156. }
  157. BENCHMARK_RELATIVE(format_nested_direct, iters) {
  158. BenchmarkSuspender suspender;
  159. while (iters--) {
  160. for (int i = 0; i < 1000; ++i) {
  161. fbstring out;
  162. suspender.dismissing([&] {
  163. format(
  164. &out,
  165. "{} {}",
  166. format("{} {}", i, i + 1),
  167. format("{} {}", -i, -i - 1));
  168. });
  169. }
  170. }
  171. }
  172. BENCHMARK_DRAW_LINE();
  173. BENCHMARK(copy_short_string, iters) {
  174. BenchmarkSuspender suspender;
  175. auto const& shortString = getShortString();
  176. while (iters--) {
  177. fbstring out;
  178. suspender.dismissing([&] { out = shortString; });
  179. }
  180. }
  181. BENCHMARK_RELATIVE(format_short_string_unsafe, iters) {
  182. BenchmarkSuspender suspender;
  183. auto const& shortString = getShortString();
  184. while (iters--) {
  185. fbstring out;
  186. suspender.dismissing([&] { format(&out, shortString); });
  187. }
  188. }
  189. BENCHMARK_RELATIVE(format_short_string_safe, iters) {
  190. BenchmarkSuspender suspender;
  191. auto const& shortString = getShortString();
  192. while (iters--) {
  193. fbstring out;
  194. suspender.dismissing([&] { format(&out, "{}", shortString); });
  195. }
  196. }
  197. BENCHMARK_RELATIVE(sformat_short_string_unsafe, iters) {
  198. BenchmarkSuspender suspender;
  199. auto const& shortString = getShortString();
  200. while (iters--) {
  201. std::string out;
  202. suspender.dismissing([&] { out = sformat(shortString); });
  203. }
  204. }
  205. BENCHMARK_RELATIVE(sformat_short_string_safe, iters) {
  206. BenchmarkSuspender suspender;
  207. auto const& shortString = getShortString();
  208. while (iters--) {
  209. std::string out;
  210. suspender.dismissing([&] { out = sformat("{}", shortString); });
  211. }
  212. }
  213. BENCHMARK_DRAW_LINE();
  214. BENCHMARK(copy_long_string, iters) {
  215. BenchmarkSuspender suspender;
  216. auto const& longString = getLongString();
  217. while (iters--) {
  218. fbstring out;
  219. suspender.dismissing([&] { out = longString; });
  220. }
  221. }
  222. BENCHMARK_RELATIVE(format_long_string_unsafe, iters) {
  223. BenchmarkSuspender suspender;
  224. auto const& longString = getLongString();
  225. while (iters--) {
  226. fbstring out;
  227. suspender.dismissing([&] { format(&out, longString); });
  228. }
  229. }
  230. BENCHMARK_RELATIVE(format_long_string_safe, iters) {
  231. BenchmarkSuspender suspender;
  232. auto const& longString = getLongString();
  233. while (iters--) {
  234. fbstring out;
  235. suspender.dismissing([&] { format(&out, "{}", longString); });
  236. }
  237. }
  238. BENCHMARK_RELATIVE(sformat_long_string_unsafe, iters) {
  239. BenchmarkSuspender suspender;
  240. auto const& longString = getLongString();
  241. while (iters--) {
  242. std::string out;
  243. suspender.dismissing([&] { out = sformat(longString); });
  244. }
  245. }
  246. BENCHMARK_RELATIVE(sformat_long_string_safe, iters) {
  247. BenchmarkSuspender suspender;
  248. auto const& longString = getLongString();
  249. while (iters--) {
  250. std::string out;
  251. suspender.dismissing([&] { out = sformat("{}", longString); });
  252. }
  253. }
  254. // Benchmark results on my dev server (20-core Intel Xeon E5-2660 v2 @ 2.20GHz)
  255. //
  256. // ============================================================================
  257. // folly/test/FormatBenchmark.cpp relative time/iter iters/s
  258. // ============================================================================
  259. // octal_snprintf 79.30ns 12.61M
  260. // octal_uintToOctal 3452.19% 2.30ns 435.35M
  261. // ----------------------------------------------------------------------------
  262. // hex_snprintf 73.59ns 13.59M
  263. // hex_uintToHex 4507.53% 1.63ns 612.49M
  264. // ----------------------------------------------------------------------------
  265. // intAppend_snprintf 191.50us 5.22K
  266. // intAppend_to 552.46% 34.66us 28.85K
  267. // intAppend_format 215.76% 88.76us 11.27K
  268. // ----------------------------------------------------------------------------
  269. // bigFormat_snprintf 178.03us 5.62K
  270. // bigFormat_format 90.41% 196.91us 5.08K
  271. // ----------------------------------------------------------------------------
  272. // format_nested_strings 317.65us 3.15K
  273. // format_nested_fbstrings 99.89% 318.01us 3.14K
  274. // format_nested_direct 116.52% 272.62us 3.67K
  275. // ----------------------------------------------------------------------------
  276. // copy_short_string 28.33ns 35.30M
  277. // format_short_string_unsafe 82.51% 34.33ns 29.13M
  278. // format_short_string_safe 58.92% 48.08ns 20.80M
  279. // sformat_short_string_unsafe 73.90% 38.33ns 26.09M
  280. // sformat_short_string_safe 54.97% 51.53ns 19.41M
  281. // ----------------------------------------------------------------------------
  282. // copy_long_string 57.56ns 17.37M
  283. // format_long_string_unsafe 68.79% 83.68ns 11.95M
  284. // format_long_string_safe 69.44% 82.89ns 12.06M
  285. // sformat_long_string_unsafe 65.58% 87.77ns 11.39M
  286. // sformat_long_string_safe 68.14% 84.47ns 11.84M
  287. // ============================================================================
  288. int main(int argc, char* argv[]) {
  289. init(&argc, &argv, true);
  290. runBenchmarks();
  291. return 0;
  292. }