Belle II Software  release-05-02-19
TimeIt.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost, Thomas Hauth <thomas.hauth@kit.edu> *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #ifdef HAS_CALLGRIND
13 #include <valgrind/callgrind.h>
14 #endif
15 
16 #include <framework/logging/Logger.h>
17 
18 #include <vector>
19 #include <functional>
20 #include <chrono>
21 #include <numeric>
22 
23 namespace Belle2 {
28  namespace TrackFindingCDC {
29 
31  class TimeItResult {
32  public:
34  explicit TimeItResult(std::vector<std::chrono::duration<double> >& timeSpans) :
35  m_timeSpans(timeSpans)
36  {}
37 
39  double getSeconds(size_t iExecution) const
40  {
41  return m_timeSpans.at(iExecution).count();
42  }
43 
45  double getAverageSeconds() const
46  {
47  std::chrono::duration<double> sumTimeSpan =
48  std::accumulate(m_timeSpans.begin(), m_timeSpans.end(), std::chrono::duration<double>());
49 
50  std::chrono::duration<double> avgTimeSpan = sumTimeSpan / m_timeSpans.size();
51  return avgTimeSpan.count();
52  }
53 
55  size_t getNExecutions() const
56  { return m_timeSpans.size(); }
57 
59  void printSummary() const
60  {
61  B2INFO("First execution took " << getSeconds(0) << " seconds ");
62  B2INFO("On average execution took " << getAverageSeconds() << " seconds " <<
63  "in " << getNExecutions() << " executions.");
64  }
65 
66  private:
68  std::vector<std::chrono::duration<double> > m_timeSpans;
69 
70  };
71 
73  const std::function<void()> doNothing = []() {};
74 
76  template<class AFunction >
78  timeIt(size_t nExecutions,
79  bool activateCallgrind,
80  const AFunction& function,
81  const std::function<void()>& setUp = doNothing,
82  const std::function<void()>& tearDown = doNothing)
83  {
84  using namespace std::chrono;
85 
86  std::vector<duration<double>> timeSpans;
87  timeSpans.reserve(nExecutions);
88 
89  for (std::size_t iExecution = 0; iExecution < nExecutions; ++iExecution) {
90  setUp();
91 
92  auto now = std::chrono::high_resolution_clock::now();
93 
94  if (activateCallgrind) {
95 #ifdef HAS_CALLGRIND
96  CALLGRIND_START_INSTRUMENTATION;
97 #endif
98  }
99 
100  function();
101 
102  if (activateCallgrind) {
103 #ifdef HAS_CALLGRIND
104  CALLGRIND_STOP_INSTRUMENTATION;
105 #endif
106  }
107 
108  auto later = std::chrono::high_resolution_clock::now();
109 
110  duration<double> timeSpan = duration_cast<duration<double> >(later - now);
111  timeSpans.push_back(timeSpan);
112  tearDown();
113  }
114  return TimeItResult(timeSpans);
115  }
116  }
118 }
Belle2::TrackFindingCDC::TimeItResult::getAverageSeconds
double getAverageSeconds() const
Get the average execution time.
Definition: TimeIt.h:53
Belle2::TrackFindingCDC::TimeItResult::printSummary
void printSummary() const
Print a summary of the collected time to the console.
Definition: TimeIt.h:67
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::TimeItResult::getSeconds
double getSeconds(size_t iExecution) const
Get the time of the individual executtions.
Definition: TimeIt.h:47
Belle2::TrackFindingCDC::TimeItResult::TimeItResult
TimeItResult(std::vector< std::chrono::duration< double > > &timeSpans)
Constructor from a series of timings.
Definition: TimeIt.h:42
Belle2::TrackFindingCDC::TimeItResult::getNExecutions
size_t getNExecutions() const
Get number of executions.
Definition: TimeIt.h:63
Belle2::TrackFindingCDC::TimeItResult::m_timeSpans
std::vector< std::chrono::duration< double > > m_timeSpans
Memory for the time spans a repeated execution took.
Definition: TimeIt.h:76
Belle2::TrackFindingCDC::TimeItResult
Class to capture the time a repeated execution took.
Definition: TimeIt.h:39