]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Provide a count_cycles() function.
authorWolfgang Bangerth <bangerth@colostate.edu>
Tue, 22 Mar 2022 23:36:25 +0000 (17:36 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 22 Mar 2022 23:36:25 +0000 (17:36 -0600)
tests/performance/valgrind_instrumentation.h

index c25773ad69ac957b2de5ead92ea1860055b673eb..56300991b1dbfea1f6e9cf0a5a90ff8f66c2929a 100644 (file)
@@ -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 <typename Func>
+  inline std::uint64_t
+  count_cycles(Func &&f)
+  {
+    start_instrumentation();
+    f();
+    return stop_instrumentation();
+  }
 } // namespace CallgrindWrapper
 
 #endif

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.