KSeExpr  4.0.4.0
Timer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #include <cassert>
5 #include <chrono>
6 
7 namespace KSeExpr
8 {
9 class Timer
10 {
11  using Time = std::time_t;
12 
13 public:
14  Timer() = default;
15  void start()
16  {
17  started = true;
18  startTime = std::chrono::steady_clock::now();
19  }
20 
21  void stop()
22  {
23  started = false;
24  }
25 
26  std::chrono::steady_clock::rep elapsedTime()
27  {
28  if (!started) start();
29  stopTime = std::chrono::steady_clock::now();
30  return std::chrono::duration_cast<std::chrono::milliseconds>(stopTime - startTime).count();
31  }
32 
33 private:
34  std::chrono::steady_clock::time_point startTime, stopTime;
35  bool started {false};
36 };
37 
38 } // namespace KSeExpr
bool started
Definition: Timer.h:35
void start()
Definition: Timer.h:15
std::chrono::steady_clock::rep elapsedTime()
Definition: Timer.h:26
std::chrono::steady_clock::time_point stopTime
Definition: Timer.h:34
std::chrono::steady_clock::time_point startTime
Definition: Timer.h:34
Timer()=default
void stop()
Definition: Timer.h:21
std::time_t Time
Definition: Timer.h:11