iMSTK
Interactive Medical Simulation Toolkit
imstkTimer.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 #include <chrono>
9 #include <ctime>
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 namespace imstk
15 {
19 class StopWatch
20 {
21 public:
22  static const double wcTimerConstants[4];
23 
24  enum class TimerState
25  {
26  started,
27  stopped
28  };
29 
30  enum class TimeUnitType
31  {
32  milliSeconds,
33  seconds,
34  minutes,
35  hours
36  };
37 
38  StopWatch() : state(TimerState::stopped) {};
39  ~StopWatch() = default;
40 
41 public:
45  virtual void start();
46 
50  virtual void stop();
51 
55  void storeLap(std::string const& lapName);
56  void storeLap();
57 
61  void printLapTimes();
62 
66  virtual void reset();
67 
71  double getTotalLapTimes();
72 
76  virtual double getTimeElapsed(const TimeUnitType unitType = TimeUnitType::milliSeconds);
77 
81  static std::string getTimeAndDate();
82 
86  void printTimeElapsed(std::string const& name = std::string("noName"), const TimeUnitType unitType = TimeUnitType::milliSeconds);
87 private:
88  TimerState state;
89  std::vector<double> lapTimes;
90  std::vector<std::string> lapNames;
91  std::chrono::high_resolution_clock::time_point wallClockTimeKeeper;
92 };
93 
97 class CpuTimer : public StopWatch
98 {
99 public:
100  static const double cpuTimerConstants[4];
101 
105  CpuTimer() : StopWatch(), cpuTimeKeeper(std::clock_t()) {};
106 
110  ~CpuTimer() = default;
111 
115  virtual void start() override { cpuTimeKeeper = std::clock(); };
116 
122  double getTimeElapsed(const TimeUnitType unitType = TimeUnitType::milliSeconds) override;
123 
124 private:
125  std::clock_t cpuTimeKeeper;
126 };
127 
134 {
135 public:
139  UPSCounter() : m_timer(std::make_shared<StopWatch>()),
140  m_accumulatedTimer(0.),
141  m_ups(0),
142  m_updateCount(0) {};
143 
144  ~UPSCounter() = default;
145 
149  void reset();
150 
154  void setStartPointOfUpdate() { m_timer->start(); }
155 
159  void setEndPointOfUpdate();
160 
164  unsigned int getUPS() const { return m_ups; }
165 
166 protected:
167 
168  std::shared_ptr<StopWatch> m_timer;
169 
171  unsigned int m_ups;
172  unsigned int m_updateCount;
173 };
174 } // namespace imstk
unsigned int getUPS() const
Get the updates per second.
Definition: imstkTimer.h:164
double getTotalLapTimes()
Returns the total time from all.
Definition: imstkTimer.cpp:50
virtual void start()
Start the appropriate timer.
Definition: imstkTimer.cpp:29
unsigned int m_ups
Most up-to-date ups.
Definition: imstkTimer.h:171
Compound Geometry.
void setStartPointOfUpdate()
Set the start point to the update.
Definition: imstkTimer.h:154
double m_accumulatedTimer
Accumulated time (always < 1 sec)
Definition: imstkTimer.h:170
UPSCounter()
Constructor/Destructor.
Definition: imstkTimer.h:139
unsigned int m_updateCount
Current update count.
Definition: imstkTimer.h:172
CPU timer.
Definition: imstkTimer.h:97
void printTimeElapsed(std::string const &name=std::string("noName"), const TimeUnitType unitType=TimeUnitType::milliSeconds)
Print the elapsed time.
Definition: imstkTimer.cpp:108
virtual void stop()
Start the appropriate timer.
Definition: imstkTimer.cpp:36
Stop Watch utility class.
Definition: imstkTimer.h:19
static std::string getTimeAndDate()
Returns a string with current date and time in UTC.
Definition: imstkTimer.cpp:101
virtual void start() override
Start the appropriate timer.
Definition: imstkTimer.h:115
void printLapTimes()
Print all the lap times.
Definition: imstkTimer.cpp:91
std::shared_ptr< StopWatch > m_timer
Timer.
Definition: imstkTimer.h:168
Utility class to count updates per second.
Definition: imstkTimer.h:133
CpuTimer()
Constructor.
Definition: imstkTimer.h:105
virtual double getTimeElapsed(const TimeUnitType unitType=TimeUnitType::milliSeconds)
Returns the time elapsed since calling start.
Definition: imstkTimer.cpp:136
virtual void reset()
Clears all the laps.
Definition: imstkTimer.cpp:43