2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Format.h>
19 #include <glog/logging.h>
21 #include <folly/FBVector.h>
22 #include <folly/Benchmark.h>
23 #include <folly/dynamic.h>
24 #include <folly/json.h>
26 using namespace folly;
34 BENCHMARK(octal_sprintf, iters) {
36 sprintf(bigBuf, "%o", static_cast<unsigned int>(iters));
40 BENCHMARK_RELATIVE(octal_uintToOctal, iters) {
42 detail::uintToOctal(bigBuf, detail::kMaxOctalLength,
43 static_cast<unsigned int>(iters));
49 BENCHMARK(hex_sprintf, iters) {
51 sprintf(bigBuf, "%x", static_cast<unsigned int>(iters));
55 BENCHMARK_RELATIVE(hex_uintToHex, iters) {
57 detail::uintToHexLower(bigBuf, detail::kMaxHexLength,
58 static_cast<unsigned int>(iters));
64 BENCHMARK(intAppend_sprintf) {
66 for (int i = -1000; i < 1000; i++) {
67 sprintf(bigBuf, "%d", i);
72 BENCHMARK_RELATIVE(intAppend_to) {
74 for (int i = -1000; i < 1000; i++) {
79 BENCHMARK_RELATIVE(intAppend_format) {
81 for (int i = -1000; i < 1000; i++) {
82 format(&out, "{}", i);
88 BENCHMARK(bigFormat_sprintf, iters) {
90 for (int i = -100; i < 100; i++) {
96 i, i+1, i+2, i+3, i+4,
97 i+5, i+6, i+7, i+8, i+9,
98 i+10, i+11, i+12, i+13, i+14,
99 i+15, i+16, i+17, i+18, i+19);
104 BENCHMARK_RELATIVE(bigFormat_format, iters) {
106 auto writeToBuf = [&p] (StringPiece sp) mutable {
107 memcpy(p, sp.data(), sp.size());
112 for (int i = -100; i < 100; i++) {
114 format("{} {} {} {} {}"
118 i, i+1, i+2, i+3, i+4,
119 i+5, i+6, i+7, i+8, i+9,
120 i+10, i+11, i+12, i+13, i+14,
121 i+15, i+16, i+17, i+18, i+19)(writeToBuf);
126 BENCHMARK_DRAW_LINE()
128 BENCHMARK(format_nested_strings, iters) {
131 for (int i = 0; i < 1000; ++i) {
133 format(&out, "{} {}",
134 format("{} {}", i, i + 1).str(),
135 format("{} {}", -i, -i - 1).str());
140 BENCHMARK_RELATIVE(format_nested_fbstrings, iters) {
143 for (int i = 0; i < 1000; ++i) {
145 format(&out, "{} {}",
146 format("{} {}", i, i + 1).fbstr(),
147 format("{} {}", -i, -i - 1).fbstr());
152 BENCHMARK_RELATIVE(format_nested_direct, iters) {
155 for (int i = 0; i < 1000; ++i) {
157 format(&out, "{} {}",
158 format("{} {}", i, i + 1),
159 format("{} {}", -i, -i - 1));
164 // Benchmark results on my dev server (dual-CPU Xeon L5520 @ 2.7GHz)
166 // ============================================================================
167 // folly/test/FormatTest.cpp relative ns/iter iters/s
168 // ============================================================================
169 // octal_sprintf 100.57 9.94M
170 // octal_uintToOctal 2599.47% 3.87 258.46M
171 // ----------------------------------------------------------------------------
172 // hex_sprintf 100.13 9.99M
173 // hex_uintToHex 3331.75% 3.01 332.73M
174 // ----------------------------------------------------------------------------
175 // intAppend_sprintf 406.07K 2.46K
176 // intAppend_to 166.03% 244.58K 4.09K
177 // intAppend_format 147.57% 275.17K 3.63K
178 // ----------------------------------------------------------------------------
179 // bigFormat_sprintf 255.40K 3.92K
180 // bigFormat_format 102.18% 249.94K 4.00K
181 // ============================================================================
183 int main(int argc, char *argv[]) {
184 gflags::ParseCommandLineFlags(&argc, &argv, true);