11#include <valgrind/callgrind.h>
14#include <framework/logging/Logger.h>
26 namespace TrackFindingCDC {
32 explicit TimeItResult(
const std::vector<std::chrono::duration<double> >& timeSpans) :
45 std::chrono::duration<double> sumTimeSpan =
48 std::chrono::duration<double> avgTimeSpan = sumTimeSpan /
m_timeSpans.size();
49 return avgTimeSpan.count();
59 B2INFO(
"First execution took " <<
getSeconds(0) <<
" seconds ");
71 const std::function<void()> doNothing = []() {};
74 template<
class AFunction >
76 timeIt(
size_t nExecutions,
77 bool activateCallgrind,
78 const AFunction& function,
79 const std::function<
void()>& setUp = doNothing,
80 const std::function<
void()>& tearDown = doNothing)
82 using namespace std::chrono;
84 std::vector<duration<double>> timeSpans;
85 timeSpans.reserve(nExecutions);
87 for (std::size_t iExecution = 0; iExecution < nExecutions; ++iExecution) {
90 auto now = std::chrono::high_resolution_clock::now();
92 if (activateCallgrind) {
94 CALLGRIND_START_INSTRUMENTATION;
100 if (activateCallgrind) {
102 CALLGRIND_STOP_INSTRUMENTATION;
106 auto later = std::chrono::high_resolution_clock::now();
108 duration<double> timeSpan = duration_cast<duration<double> >(later - now);
109 timeSpans.push_back(timeSpan);
112 return TimeItResult(timeSpans);
Class to capture the time a repeated execution took.
void printSummary() const
Print a summary of the collected time to the console.
size_t getNExecutions() const
Get number of executions.
double getSeconds(size_t iExecution) const
Get the time of the individual executions.
double getAverageSeconds() const
Get the average execution time.
TimeItResult(const std::vector< std::chrono::duration< double > > &timeSpans)
Constructor from a series of timings.
std::vector< std::chrono::duration< double > > m_timeSpans
Memory for the time spans a repeated execution took.
Abstract base class for different kinds of events.