File: timer.hpp

package info (click to toggle)
libbiosoup-dev 0.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: cpp: 646; makefile: 12
file content (63 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2020 Robert Vaser

#ifndef BIOSOUP_TIMER_HPP_
#define BIOSOUP_TIMER_HPP_

#include <chrono>  // NOLINT
#include <cstdint>

namespace biosoup {

class Timer {
 public:
  Timer()
      : checkpoint_(), elapsed_time_(0) {}

  Timer(const Timer&) = default;
  Timer& operator=(const Timer&) = default;

  Timer(Timer&&) = default;
  Timer& operator=(Timer&&) = default;

  ~Timer() = default;

  double elapsed_time(void) const {
    return elapsed_time_;
  }

  void Start() {
    checkpoint_ = std::chrono::steady_clock::now();
  }

  double Stop() {
    if (checkpoint_.time_since_epoch().count()) {  // Start() was called
      auto duration = std::chrono::duration_cast<std::chrono::duration<double>>(
          std::chrono::steady_clock::now() - checkpoint_).count();
      checkpoint_ = {};
      elapsed_time_ += duration;
      return duration;
    }
    return 0;
  }

  double Lap() const {
    if (checkpoint_.time_since_epoch().count()) {  // Start() was called
      return std::chrono::duration_cast<std::chrono::duration<double>>(
          std::chrono::steady_clock::now() - checkpoint_).count();
    }
    return 0;
  }

  void Reset() {
    checkpoint_ = {};
    elapsed_time_ = 0;
  }

 private:
  std::chrono::time_point<std::chrono::steady_clock> checkpoint_;
  double elapsed_time_;
};

}  // namespace biosoup

#endif  // BIOSOUP_TIMER_HPP_