diff --git a/folly/Benchmark.h b/folly/Benchmark.h index dd11a00..3478d10 100644 --- a/folly/Benchmark.h +++ b/folly/Benchmark.h @@ -17,6 +17,7 @@ #ifndef FOLLY_BENCHMARK_H_ #define FOLLY_BENCHMARK_H_ +#include #include #include // for FB_ANONYMOUS_VARIABLE #include @@ -26,6 +27,7 @@ #include #include #include +#include DECLARE_bool(benchmark); @@ -149,6 +151,13 @@ struct BenchmarkSuspender { CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &start)); } + template + auto dismissing(F f) -> typename std::result_of::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. diff --git a/folly/test/BenchmarkTest.cpp b/folly/test/BenchmarkTest.cpp index 4b4d208..116ecb3 100644 --- a/folly/test/BenchmarkTest.cpp +++ b/folly/test/BenchmarkTest.cpp @@ -17,7 +17,11 @@ #include #include #include +#include #include +#include +#include +#include using namespace folly; using namespace std; @@ -128,6 +132,38 @@ BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 1); 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 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 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();