/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
#include <atomic>
+
#include <glog/logging.h>
#include <folly/Benchmark.h>
| as<vector>();
auto square = [](int x) { return x * x; };
-auto add = [](int a, int b) { return a + b; };
-auto multiply = [](int a, int b) { return a * b; };
BENCHMARK(Sum_Basic_NoGen, iters) {
int limit = testSize.load();
BENCHMARK(Fib_Sum_NoGen, iters) {
int s = 0;
while (iters--) {
- auto fib = [](int limit) -> vector<int> {
+ auto fib = [](size_t limit) -> vector<int> {
vector<int> ret;
int a = 0;
int b = 1;
- for (int i = 0; i * 2 < limit; ++i) {
+ for (size_t i = 0; i < limit; i += 2) {
ret.push_back(a += b);
ret.push_back(b += a);
}
yield(b += a);
}
};
+ // Early stopping implemented with exceptions.
s += fib | take(testSize.load()) | sum;
}
folly::doNotOptimizeAway(s);
}
+BENCHMARK_RELATIVE(Fib_Sum_Gen_Limit, iters) {
+ int s = 0;
+ while (iters--) {
+ size_t limit = testSize.load();
+ auto fib = GENERATOR(int) {
+ int a = 0;
+ int b = 1;
+ for (size_t i = 0; i < limit; i += 2) {
+ yield(a += b);
+ yield(b += a);
+ }
+ };
+ // No early stopping.
+ s += fib | sum;
+ }
+ folly::doNotOptimizeAway(s);
+}
+
struct FibYielder {
- template<class Yield>
+ template <class Yield>
void operator()(Yield&& yield) const {
int a = 0;
int b = 1;