The Gaudi Framework  v32r2 (46d42edc)
GenericTimer.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <atomic>
6 #include <chrono>
7 
8 namespace Gaudi {
9  namespace Timers {
32  template <typename Clock, typename Unit>
33  class GenericTimer {
34  public:
37 
41  class ScopeTimer {
42  public:
44  ScopeTimer( Stats_t& stat ) : m_stats( stat ), m_t0( Clock::now() ) {}
45 
47  ~ScopeTimer() { stop(); }
48 
51  auto elapsed() const {
52  // Fenced according to https://codereview.stackexchange.com/q/196245
53  std::atomic_thread_fence( std::memory_order_relaxed );
54  auto dt = Clock::now() - m_t0;
55  std::atomic_thread_fence( std::memory_order_relaxed );
56  return std::chrono::duration_cast<Unit>( dt );
57  }
58 
59  private:
61  void stop() const { m_stats += elapsed(); }
62 
64  typename Clock::time_point m_t0;
65  };
66 
68  GenericTimer() = default;
69 
71  template <class OWNER>
72  GenericTimer( OWNER* o, const std::string& name ) {
73  o->declareCounter( name, m_stats );
74  }
75 
77  GenericTimer( const GenericTimer& ) = delete;
78 
80  [[nodiscard]] auto operator()() const { return ScopeTimer( m_stats ); }
81 
83  const Stats_t& stats() const { return m_stats; }
84 
85  private:
86  mutable Stats_t m_stats;
87  };
88 
89  } // namespace Timers
90 } // namespace Gaudi
auto elapsed() const
Return the elapsed time without stopping the timer.
Definition: GenericTimer.h:51
A generic timer based on std::chrono and Gaudi::Accumulators.
Definition: GenericTimer.h:33
A counter aiming at computing average and sum2 / variance / standard deviation.
Definition: Counters.h:744
GenericTimer()=default
Default constructor.
const Stats_t & stats() const
Return accumulated timing statistics.
Definition: GenericTimer.h:83
GenericTimer(OWNER *o, const std::string &name)
Constructor attaching the statistics counter to an owner.
Definition: GenericTimer.h:72
STL class.
Clock::time_point m_t0
start time of timer
Definition: GenericTimer.h:64
Gaudi::Accumulators::StatCounter< Unit > Stats_t
Type of Counter used for accumulating time measurements.
Definition: GenericTimer.h:36
void stop() const
Stop the timer, accumulate elapsed time and return current measurement.
Definition: GenericTimer.h:61
A scoped timer that starts/stops on con/de-struction.
Definition: GenericTimer.h:41
auto operator()() const
Create (and start) a ScopeTimer.
Definition: GenericTimer.h:80
Stats_t m_stats
statistics counter
Definition: GenericTimer.h:86
T atomic_thread_fence(T... args)
ScopeTimer(Stats_t &stat)
Start Scoped timer accumulating into stat.
Definition: GenericTimer.h:44
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Stats_t & m_stats
reference to statistics counter
Definition: GenericTimer.h:63
~ScopeTimer()
Destructor stopping timer.
Definition: GenericTimer.h:47