2 * Copyright 2015 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 <glog/logging.h>
19 #include <folly/Benchmark.h>
20 #include <folly/gen/Base.h>
22 using namespace folly::gen;
23 using folly::fbstring;
29 static std::atomic<int> testSize(1000);
30 static vector<int> testVector =
31 seq(1, testSize.load())
32 | mapped([](int) { return rand(); })
35 static vector<vector<int>> testVectorVector =
38 return seq(1, i) | as<vector>();
41 static vector<fbstring> strings =
46 auto square = [](int x) { return x * x; };
47 auto add = [](int a, int b) { return a + b; };
48 auto multiply = [](int a, int b) { return a * b; };
50 BENCHMARK(Sum_Basic_NoGen, iters) {
51 int limit = testSize.load();
54 for (int i = 0; i < limit; ++i) {
58 folly::doNotOptimizeAway(s);
61 BENCHMARK_RELATIVE(Sum_Basic_Gen, iters) {
62 int limit = testSize.load();
65 s += range(0, limit) | sum;
67 folly::doNotOptimizeAway(s);
72 BENCHMARK(Sum_Vector_NoGen, iters) {
75 for (auto& i : testVector) {
79 folly::doNotOptimizeAway(s);
82 BENCHMARK_RELATIVE(Sum_Vector_Gen, iters) {
85 s += from(testVector) | sum;
87 folly::doNotOptimizeAway(s);
92 BENCHMARK(Member, iters) {
96 | member(&fbstring::size)
99 folly::doNotOptimizeAway(s);
102 BENCHMARK_RELATIVE(MapMember, iters) {
106 | map([](const fbstring& x) { return x.size(); })
109 folly::doNotOptimizeAway(s);
112 BENCHMARK_DRAW_LINE()
114 BENCHMARK(Count_Vector_NoGen, iters) {
117 for (auto& i : testVector) {
118 if (i * 2 < rand()) {
123 folly::doNotOptimizeAway(s);
126 BENCHMARK_RELATIVE(Count_Vector_Gen, iters) {
129 s += from(testVector)
131 return i * 2 < rand();
135 folly::doNotOptimizeAway(s);
138 BENCHMARK_DRAW_LINE()
140 BENCHMARK(Fib_Sum_NoGen, iters) {
143 auto fib = [](int limit) -> vector<int> {
147 for (int i = 0; i * 2 < limit; ++i) {
148 ret.push_back(a += b);
149 ret.push_back(b += a);
153 for (auto& v : fib(testSize.load())) {
157 folly::doNotOptimizeAway(s);
160 BENCHMARK_RELATIVE(Fib_Sum_Gen, iters) {
163 auto fib = GENERATOR(int) {
171 s += fib | take(testSize.load()) | sum;
173 folly::doNotOptimizeAway(s);
177 template<class Yield>
178 void operator()(Yield&& yield) const {
188 BENCHMARK_RELATIVE(Fib_Sum_Gen_Static, iters) {
191 auto fib = generator<int>(FibYielder());
192 s += fib | take(testSize.load()) | sum;
194 folly::doNotOptimizeAway(s);
197 BENCHMARK_DRAW_LINE()
199 BENCHMARK(VirtualGen_0Virtual, iters) {
202 auto numbers = seq(1, 10000);
203 auto squares = numbers | map(square);
204 auto quads = squares | map(square);
207 folly::doNotOptimizeAway(s);
210 BENCHMARK_RELATIVE(VirtualGen_1Virtual, iters) {
213 VirtualGen<int> numbers = seq(1, 10000);
214 auto squares = numbers | map(square);
215 auto quads = squares | map(square);
218 folly::doNotOptimizeAway(s);
221 BENCHMARK_RELATIVE(VirtualGen_2Virtual, iters) {
224 VirtualGen<int> numbers = seq(1, 10000);
225 VirtualGen<int> squares = numbers | map(square);
226 auto quads = squares | map(square);
229 folly::doNotOptimizeAway(s);
232 BENCHMARK_RELATIVE(VirtualGen_3Virtual, iters) {
235 VirtualGen<int> numbers = seq(1, 10000);
236 VirtualGen<int> squares = numbers | map(square);
237 VirtualGen<int> quads = squares | map(square);
240 folly::doNotOptimizeAway(s);
243 BENCHMARK_DRAW_LINE()
245 BENCHMARK(Concat_NoGen, iters) {
248 for (auto& v : testVectorVector) {
254 folly::doNotOptimizeAway(s);
257 BENCHMARK_RELATIVE(Concat_Gen, iters) {
260 s += from(testVectorVector) | rconcat | sum;
262 folly::doNotOptimizeAway(s);
265 BENCHMARK_DRAW_LINE()
267 BENCHMARK(Composed_NoGen, iters) {
270 for (auto& i : testVector) {
274 folly::doNotOptimizeAway(s);
277 BENCHMARK_RELATIVE(Composed_Gen, iters) {
279 auto sumSq = map(square) | sum;
281 s += from(testVector) | sumSq;
283 folly::doNotOptimizeAway(s);
286 BENCHMARK_RELATIVE(Composed_GenRegular, iters) {
289 s += from(testVector) | map(square) | sum;
291 folly::doNotOptimizeAway(s);
294 BENCHMARK_DRAW_LINE()
296 BENCHMARK(Sample, iters) {
299 auto sampler = seq(1, 10 * 1000 * 1000) | sample(1000);
300 s += (sampler | sum);
302 folly::doNotOptimizeAway(s);
305 // Results from an Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
306 // ============================================================================
307 // folly/gen/test/BaseBenchmark.cpp relative time/iter iters/s
308 // ============================================================================
309 // Sum_Basic_NoGen 372.39ns 2.69M
310 // Sum_Basic_Gen 195.96% 190.03ns 5.26M
311 // ----------------------------------------------------------------------------
312 // Sum_Vector_NoGen 200.41ns 4.99M
313 // Sum_Vector_Gen 77.14% 259.81ns 3.85M
314 // ----------------------------------------------------------------------------
315 // Member 4.56us 219.42K
316 // MapMember 400.47% 1.14us 878.73K
317 // ----------------------------------------------------------------------------
318 // Count_Vector_NoGen 13.96us 71.64K
319 // Count_Vector_Gen 86.05% 16.22us 61.65K
320 // ----------------------------------------------------------------------------
321 // Fib_Sum_NoGen 2.21us 452.63K
322 // Fib_Sum_Gen 23.94% 9.23us 108.36K
323 // Fib_Sum_Gen_Static 48.77% 4.53us 220.73K
324 // ----------------------------------------------------------------------------
325 // VirtualGen_0Virtual 9.60us 104.13K
326 // VirtualGen_1Virtual 28.00% 34.30us 29.15K
327 // VirtualGen_2Virtual 22.62% 42.46us 23.55K
328 // VirtualGen_3Virtual 16.96% 56.64us 17.66K
329 // ----------------------------------------------------------------------------
330 // Concat_NoGen 2.20us 453.66K
331 // Concat_Gen 109.49% 2.01us 496.70K
332 // ----------------------------------------------------------------------------
333 // Composed_NoGen 545.32ns 1.83M
334 // Composed_Gen 87.94% 620.07ns 1.61M
335 // Composed_GenRegular 88.13% 618.74ns 1.62M
336 // ----------------------------------------------------------------------------
337 // Sample 176.48ms 5.67
338 // ============================================================================
340 int main(int argc, char *argv[]) {
341 gflags::ParseCommandLineFlags(&argc, &argv, true);
342 folly::runBenchmarks();