From: Wolfgang Bangerth Date: Tue, 22 Mar 2022 23:36:25 +0000 (-0600) Subject: Provide a count_cycles() function. X-Git-Tag: v9.4.0-rc1~295^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5642bdeeb1bdb400fc53244783d6ed9681ff0ddb;p=dealii.git Provide a count_cycles() function. --- 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