Jlm
time.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Nico Reißmann <nico.reissmann@gmail.com>
3  * Copyright 2024 Håvard Krogstie <krogstie.havard@gmail.com>
4  * See COPYING for terms of redistribution.
5  */
6 
7 #ifndef JLM_UTIL_TIME_HPP
8 #define JLM_UTIL_TIME_HPP
9 
10 #include <jlm/util/common.hpp>
11 
12 #include <chrono>
13 
14 namespace jlm::util
15 {
16 
17 class Timer final
18 {
19 public:
20  constexpr Timer()
22  IsRunning_(false)
23  {}
24 
25  Timer(const Timer & other) = delete;
26  Timer(Timer && other) = default;
27  Timer &
28  operator=(const Timer & other) = delete;
29  Timer &
30  operator=(Timer && other) = default;
31 
32  [[nodiscard]] bool
33  isRunning() const noexcept
34  {
35  return IsRunning_;
36  }
37 
42  void
43  reset() noexcept
44  {
46  IsRunning_ = false;
47  }
48 
53  void
54  start() noexcept
55  {
56  if (IsRunning_)
57  return;
58  Start_ = std::chrono::high_resolution_clock::now();
59  IsRunning_ = true;
60  }
61 
66  void
67  stop() noexcept
68  {
69  if (!IsRunning_)
70  return;
71  auto end = std::chrono::high_resolution_clock::now();
73  std::chrono::duration_cast<std::chrono::nanoseconds>(end - Start_).count();
74  IsRunning_ = false;
75  }
76 
82  [[nodiscard]] size_t
83  ns() const
84  {
85  if (IsRunning_)
86  throw std::logic_error("Timer is running");
88  }
89 
90 private:
92  bool IsRunning_;
93  std::chrono::time_point<std::chrono::high_resolution_clock> Start_;
94 };
95 
96 }
97 
98 #endif
size_t ElapsedTimeInNanoseconds_
Definition: time.hpp:91
std::chrono::time_point< std::chrono::high_resolution_clock > Start_
Definition: time.hpp:93
void reset() noexcept
Definition: time.hpp:43
Timer(const Timer &other)=delete
void start() noexcept
Definition: time.hpp:54
void stop() noexcept
Definition: time.hpp:67
Timer & operator=(const Timer &other)=delete
Timer(Timer &&other)=default
Timer & operator=(Timer &&other)=default
bool IsRunning_
Definition: time.hpp:92
size_t ns() const
Definition: time.hpp:83
bool isRunning() const noexcept
Definition: time.hpp:33
constexpr Timer()
Definition: time.hpp:20