From 5642bdeeb1bdb400fc53244783d6ed9681ff0ddb Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Tue, 22 Mar 2022 17:36:25 -0600 Subject: [PATCH] Provide a count_cycles() function. --- tests/performance/valgrind_instrumentation.h | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/performance/valgrind_instrumentation.h b/tests/performance/valgrind_instrumentation.h index c25773ad69..56300991b1 100644 --- a/tests/performance/valgrind_instrumentation.h +++ b/tests/performance/valgrind_instrumentation.h @@ -138,6 +138,36 @@ namespace CallgrindWrapper return cycles; } + + + /** + * A function that counts the cycles necessary to execute the given function + * argument. The following are therefore equivalent: + * @code + * start_instrumentation(); + * my_function(); + * const auto cycles = stop_instrumentation(); + * @endcode + * and + * @code + * const auto cycles = count_cycles([](){ my_function(); }); + * @endcode + * If the call to `my_function()` involves arguments or code pieces that + * access variables in the environment of the place where the function is + * called, then one can of course capture these variables in the construction + * of the lambda function and instead call variations such as + * @code + * const auto cycles = count_cycles([&](){ my_function(); }); + * @endcode + */ + template + inline std::uint64_t + count_cycles(Func &&f) + { + start_instrumentation(); + f(); + return stop_instrumentation(); + } } // namespace CallgrindWrapper #endif -- 2.39.5