carl  25.02
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::micro>;
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 } // namespace timing
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()/1000;
62  }
63  auto overall_us() const {
64  return m_overall.count();
65  }
66 
67  void collect(std::map<std::string, std::string>& data, const std::string& key) {
68  bool active_at_timeout = check_finish();
69  data.emplace(key + ".count", std::to_string(count()));
70  data.emplace(key + ".overall_ms", std::to_string(overall_ms()));
71  data.emplace(key + ".overall_µs", std::to_string(overall_us()));
72  data.emplace(key + ".active_at_timeout", active_at_timeout ? "1" : "0");
73  }
74 };
75 
76 } // namespace statistics
77 
78 } // namespace carl
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
std::chrono::duration< std::size_t, std::micro > duration
The duration type used here.
Definition: Timing.h:12
auto zero()
Return a zero duration.
Definition: Timing.h:25
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
auto overall_us() const
Definition: Timing.h:63
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:67
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