2 * Copyright 2012 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/experimental/Gen.h"
19 #include <glog/logging.h>
22 #include "folly/Benchmark.h"
24 using namespace folly;
25 using namespace folly::gen;
32 static std::atomic<int> testSize(1000);
33 static vector<int> testVector =
34 seq(1, testSize.load())
35 | mapped([](int) { return rand(); })
37 static vector<vector<int>> testVectorVector =
40 return seq(1, i) | as<vector>();
44 auto square = [](int x) { return x * x; };
45 auto add = [](int a, int b) { return a + b; };
46 auto multiply = [](int a, int b) { return a * b; };
48 BENCHMARK(Sum_Basic_NoGen, iters) {
49 int limit = testSize.load();
52 for (int i = 0; i < limit; ++i) {
56 folly::doNotOptimizeAway(s);
59 BENCHMARK_RELATIVE(Sum_Basic_Gen, iters) {
60 int limit = testSize.load();
63 s += range(0, limit) | sum;
65 folly::doNotOptimizeAway(s);
70 BENCHMARK(Sum_Vector_NoGen, iters) {
73 for (auto& i : testVector) {
77 folly::doNotOptimizeAway(s);
80 BENCHMARK_RELATIVE(Sum_Vector_Gen, iters) {
83 s += from(testVector) | sum;
85 folly::doNotOptimizeAway(s);
90 BENCHMARK(Count_Vector_NoGen, iters) {
93 for (auto& i : testVector) {
99 folly::doNotOptimizeAway(s);
102 BENCHMARK_RELATIVE(Count_Vector_Gen, iters) {
105 s += from(testVector)
107 return i * 2 < rand();
111 folly::doNotOptimizeAway(s);
114 BENCHMARK_DRAW_LINE()
116 BENCHMARK(Fib_Sum_NoGen, iters) {
119 auto fib = [](int limit) -> vector<int> {
123 for (int i = 0; i * 2 < limit; ++i) {
124 ret.push_back(a += b);
125 ret.push_back(b += a);
129 for (auto& v : fib(testSize.load())) {
134 folly::doNotOptimizeAway(s);
137 BENCHMARK_RELATIVE(Fib_Sum_Gen, iters) {
140 auto fib = GENERATOR(int, {
148 s += fib | take(testSize.load()) | sum;
150 folly::doNotOptimizeAway(s);
154 template<class Yield>
155 void operator()(Yield&& yield) const {
165 BENCHMARK_RELATIVE(Fib_Sum_Gen_Static, iters) {
168 auto fib = generator<int>(FibYielder());
169 s += fib | take(30) | sum;
171 folly::doNotOptimizeAway(s);
174 BENCHMARK_DRAW_LINE()
176 BENCHMARK(VirtualGen_0Virtual, iters) {
179 auto numbers = seq(1, 10000);
180 auto squares = numbers | map(square);
181 auto quads = squares | map(square);
184 folly::doNotOptimizeAway(s);
187 BENCHMARK_RELATIVE(VirtualGen_1Virtual, iters) {
190 VirtualGen<int> numbers = seq(1, 10000);
191 auto squares = numbers | map(square);
192 auto quads = squares | map(square);
195 folly::doNotOptimizeAway(s);
198 BENCHMARK_RELATIVE(VirtualGen_2Virtual, iters) {
201 VirtualGen<int> numbers = seq(1, 10000);
202 VirtualGen<int> squares = numbers | map(square);
203 auto quads = squares | map(square);
206 folly::doNotOptimizeAway(s);
209 BENCHMARK_RELATIVE(VirtualGen_3Virtual, iters) {
212 VirtualGen<int> numbers = seq(1, 10000);
213 VirtualGen<int> squares = numbers | map(square);
214 VirtualGen<int> quads = squares | map(square);
217 folly::doNotOptimizeAway(s);
220 BENCHMARK_DRAW_LINE()
222 BENCHMARK(Concat_NoGen, iters) {
225 for (auto& v : testVectorVector) {
231 folly::doNotOptimizeAway(s);
234 BENCHMARK_RELATIVE(Concat_Gen, iters) {
237 s += from(testVectorVector) | rconcat | sum;
239 folly::doNotOptimizeAway(s);
242 // Results from a dual core Xeon L5520 @ 2.27GHz:
244 // ============================================================================
245 // folly/experimental/test/GenBenchmark.cpp relative time/iter iters/s
246 // ============================================================================
247 // Sum_Basic_NoGen 301.60ns 3.32M
248 // Sum_Basic_Gen 103.20% 292.24ns 3.42M
249 // ----------------------------------------------------------------------------
250 // Sum_Vector_NoGen 200.33ns 4.99M
251 // Sum_Vector_Gen 99.44% 201.45ns 4.96M
252 // ----------------------------------------------------------------------------
253 // Count_Vector_NoGen 19.07fs 52.43T
254 // Count_Vector_Gen 166.67% 11.44fs 87.38T
255 // ----------------------------------------------------------------------------
256 // Fib_Sum_NoGen 4.15us 241.21K
257 // Fib_Sum_Gen 48.75% 8.50us 117.58K
258 // Fib_Sum_Gen_Static 113.24% 3.66us 273.16K
259 // ----------------------------------------------------------------------------
260 // VirtualGen_0Virtual 10.05us 99.48K
261 // VirtualGen_1Virtual 29.63% 33.93us 29.47K
262 // VirtualGen_2Virtual 20.47% 49.09us 20.37K
263 // VirtualGen_3Virtual 15.30% 65.68us 15.23K
264 // ----------------------------------------------------------------------------
265 // Concat_NoGen 2.34us 427.15K
266 // Concat_Gen 90.04% 2.60us 384.59K
267 // ============================================================================
269 int main(int argc, char *argv[]) {
270 google::ParseCommandLineFlags(&argc, &argv, true);