carl  24.04
Computer ARithmetic Library
Timing.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 
5 namespace carl {
6 namespace statistics {
7 
8 namespace timing {
9  /// The clock type used here.
10  using clock = std::chrono::high_resolution_clock;
11  /// The duration type used here.
12  using duration = std::chrono::duration<std::size_t,std::milli>;
13  /// The type of a time point.
15 
16  /// Return the current time point.
17  inline auto now() {
18  return clock::now();
19  }
20  /// Return the duration since the given start time point.
21  inline auto since(time_point start) {
22  return std::chrono::duration_cast<duration>(clock::now() - start);
23  }
24  /// Return a zero duration.
25  inline auto zero() {
26  return duration::zero();
27  }
28 }
29 
30 class Timer {
31  std::size_t m_count = 0;
33  timing::time_point m_current_start = timing::time_point::min();
34 
35 public:
37  return timing::now();
38  }
40  ++m_count;
42  }
43  void start_this() {
45  }
46  void finish() {
48  m_current_start = timing::time_point::min();
49  }
50  bool check_finish() {
51  if (m_current_start != timing::time_point::min()) {
52  finish();
53  return true;
54  }
55  return false;
56  }
57  auto count() const {
58  return m_count;
59  }
60  auto overall_ms() const {
61  return m_overall.count();
62  }
63 
64  void collect(std::map<std::string, std::string>& data, const std::string& key) {
65  bool active_at_timeout = check_finish();
66  data.emplace(key+".count", std::to_string(count()));
67  data.emplace(key+".overall_ms", std::to_string(overall_ms()));
68  data.emplace(key+".active_at_timeout", active_at_timeout ? "1" : "0");
69  }
70 };
71 
72 }
73 }
carl is the main namespace for the library.
auto since(time_point start)
Return the duration since the given start time point.
Definition: Timing.h:21
clock::time_point time_point
The type of a time point.
Definition: Timing.h:14
auto zero()
Return a zero duration.
Definition: Timing.h:25
std::chrono::duration< std::size_t, std::milli > duration
The duration type used here.
Definition: Timing.h:12
auto now()
Return the current time point.
Definition: Timing.h:17
std::chrono::high_resolution_clock clock
The clock type used here.
Definition: Timing.h:10
auto count() const
Definition: Timing.h:57
static timing::time_point start()
Definition: Timing.h:36
auto overall_ms() const
Definition: Timing.h:60
void collect(std::map< std::string, std::string > &data, const std::string &key)
Definition: Timing.h:64
std::size_t m_count
Definition: Timing.h:31
timing::time_point m_current_start
Definition: Timing.h:33
void finish(timing::time_point start)
Definition: Timing.h:39
timing::duration m_overall
Definition: Timing.h:32