DiSMEC++
timer.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_STATS_TIMER_H
7 #define DISMEC_STATS_TIMER_H
8 
9 #include <chrono>
10 #include "stat_id.h"
11 
12 namespace dismec::stats {
13  class StatisticsCollection;
14 
15  class ScopeTimer {
16  using clock_t = std::chrono::high_resolution_clock;
18  bool m_Enabled = false;
19  clock_t::time_point m_Start;
21 
23 
25  m_Target(id), m_Enabled(is_enabled(accu, id)), m_Accu(accu) {
26  if(m_Enabled) {
27  m_Start = clock_t::now();
28  }
29  }
30 
31  // We provide these two functions as non-inline definitions so we don't have to include `collection.h`
32  // here.
33  void record_duration();
34  [[nodiscard]] static bool is_enabled(const StatisticsCollection* accu, stat_id_t stat);
35  public:
37  if(m_Enabled) {
39  }
40  }
41  ScopeTimer(const ScopeTimer&) = delete;
42  ScopeTimer operator=(const ScopeTimer&) = delete;
43 
47  ScopeTimer(ScopeTimer&& other) noexcept :
48  m_Target(other.m_Target), m_Enabled(other.m_Enabled), m_Start(other.m_Start), m_Accu(other.m_Accu) {
49  other.m_Enabled = false; // disable recording for other.
50  }
51  };
52 
54  return {&accumulator, id};
55  }
56 }
57 
58 #endif //DISMEC_STATS_TIMER_H
ScopeTimer(StatisticsCollection *accu, stat_id_t id)
Definition: timer.h:24
StatisticsCollection * m_Accu
Definition: timer.h:20
ScopeTimer(const ScopeTimer &)=delete
friend ScopeTimer record_scope_time(StatisticsCollection &accumulator, stat_id_t id)
Definition: timer.h:53
std::chrono::high_resolution_clock clock_t
Definition: timer.h:16
static bool is_enabled(const StatisticsCollection *accu, stat_id_t stat)
Definition: timer.cpp:16
clock_t::time_point m_Start
Definition: timer.h:19
stat_id_t m_Target
Definition: timer.h:17
ScopeTimer operator=(const ScopeTimer &)=delete
ScopeTimer(ScopeTimer &&other) noexcept
Move constructor. Needs to disable recording for the moved-from timer.
Definition: timer.h:47
This class manages a collection of named Statistics objects.
Definition: collection.h:47
ScopeTimer record_scope_time(StatisticsCollection &accumulator, stat_id_t id)
Definition: timer.h:53