#ifndef FOLLY_BENCHMARK_H_
#define FOLLY_BENCHMARK_H_
+#include <folly/ScopeGuard.h>
#include <folly/Portability.h>
#include <folly/Preprocessor.h> // for FB_ANONYMOUS_VARIABLE
#include <cassert>
#include <glog/logging.h>
#include <gflags/gflags.h>
#include <limits>
+#include <type_traits>
DECLARE_bool(benchmark);
CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &start));
}
+ template <class F>
+ auto dismissing(F f) -> typename std::result_of<F()>::type {
+ SCOPE_EXIT { rehire(); };
+ dismiss();
+ return f();
+ }
+
/**
* This is for use inside of if-conditions, used in BENCHMARK macros.
* If-conditions bypass the explicit on operator bool.
#include <folly/Benchmark.h>
#include <folly/Foreach.h>
#include <folly/String.h>
+#include <algorithm>
#include <iostream>
+#include <numeric>
+#include <random>
+#include <vector>
using namespace folly;
using namespace std;
BENCHMARK_PARAM_MULTI(paramMulti, 5);
BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 5);
+BENCHMARK_DRAW_LINE();
+
+BENCHMARK(BenchmarkSuspender_dismissing_void, iter) {
+ BenchmarkSuspender braces;
+ mt19937_64 rng;
+ while (iter--) {
+ vector<size_t> v(1 << 12, 0);
+ iota(v.begin(), v.end(), 0);
+ shuffle(v.begin(), v.end(), rng);
+ braces.dismissing([&] {
+ sort(v.begin(), v.end());
+ });
+ }
+}
+
+BENCHMARK(BenchmarkSuspender_dismissing_value, iter) {
+ BenchmarkSuspender braces;
+ mt19937_64 rng;
+ while (iter--) {
+ vector<size_t> v(1 << 12, 0);
+ iota(v.begin(), v.end(), 0);
+ shuffle(v.begin(), v.end(), rng);
+ auto s = braces.dismissing([&] {
+ sort(v.begin(), v.end());
+ return accumulate(v.begin(), v.end(), 0, [](size_t a, size_t e) {
+ return a + e;
+ });
+ });
+ doNotOptimizeAway(s);
+ }
+}
+
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
runBenchmarks();