iMSTK
Interactive Medical Simulation Toolkit
imstkTimer.cpp
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 #include "imstkTimer.h"
8 #include "imstkLogger.h"
9 
10 namespace imstk
11 {
12 const double
13 StopWatch::wcTimerConstants[] = {
14  1.0,
15  1 / 1000.0,
16  1 / (1000.0 * 60.0),
17  1 / (1000.0 * 60.0 * 60)
18 };
19 
20 const double
21 CpuTimer::cpuTimerConstants[] = {
22  1000.0 / CLOCKS_PER_SEC,
23  1.0 / CLOCKS_PER_SEC,
24  1.0 / (CLOCKS_PER_SEC * 60.0),
25  1.0 / (CLOCKS_PER_SEC * 60.0 * 60.0)
26 };
27 
28 void
30 {
31  wallClockTimeKeeper = std::chrono::high_resolution_clock::now();
32  this->state = TimerState::started;
33 }
34 
35 void
37 {
38  this->storeLap();
39  this->state = TimerState::stopped;
40 }
41 
42 void
44 {
45  lapTimes.clear();
46  this->state = TimerState::stopped;
47 }
48 
49 double
51 {
52  double totalTime = 0.0;
53  for (const auto& timeChunk : lapTimes)
54  {
55  totalTime += timeChunk;
56  }
57  return totalTime;
58 }
59 
60 void
61 StopWatch::storeLap(std::string const& lapName)
62 {
63  if (this->lapTimes.size() > 0)
64  {
65  this->lapTimes.push_back(this->getTimeElapsed() - *this->lapTimes.end());
66  }
67  else
68  {
69  this->lapTimes.push_back(this->getTimeElapsed());
70  }
71 
72  if (lapName == "noName")
73  {
74  this->lapNames.push_back("Lap " + std::to_string(lapNames.size()));
75  }
76  else
77  {
78  this->lapNames.push_back(lapName);
79  }
80 
81  this->state = TimerState::stopped;
82 }
83 
84 void
85 StopWatch::storeLap()
86 {
87  this->storeLap(std::string("noName"));
88 }
89 
90 void
92 {
93  LOG(INFO) << "Lap times:";
94  for (size_t i = 0; i < this->lapTimes.size(); ++i)
95  {
96  LOG(INFO) << "\t" << this->lapNames.at(i) << ": " << this->lapTimes.at(i) << "ms";
97  }
98 }
99 
100 std::string
102 {
103  time_t now = time(0);
104  return std::string(asctime(gmtime(&now)));
105 }
106 
107 void
108 StopWatch::printTimeElapsed(std::string const& name /* = std::string("noName")*/,
109  const TimeUnitType unitType /*= TimeUnitType::milliSeconds*/)
110 {
111  switch (unitType)
112  {
113  case TimeUnitType::milliSeconds:
114  LOG(INFO) << name << ": " << this->getTimeElapsed(unitType) << " ms.";
115  break;
116 
117  case TimeUnitType::seconds:
118  LOG(INFO) << name << ": " << this->getTimeElapsed(unitType) << " sec.";
119  break;
120 
121  case TimeUnitType::minutes:
122  LOG(INFO) << name << ": " << this->getTimeElapsed(unitType) << " min.";
123  break;
124 
125  case TimeUnitType::hours:
126  LOG(INFO) << name << ": " << this->getTimeElapsed(unitType) << " hrs.";
127  break;
128 
129  default:
130  LOG(WARNING) << "Type of the time integrator not identified!";
131  break;
132  }
133 }
134 
135 double
136 StopWatch::getTimeElapsed(const TimeUnitType unitType /*= TimeUnitType::milliSeconds*/)
137 {
138  return std::chrono::duration<double, std::milli>
139  (std::chrono::high_resolution_clock::now() - wallClockTimeKeeper).count() *
140  wcTimerConstants[(int)unitType];
141 }
142 
143 double
144 CpuTimer::getTimeElapsed(const TimeUnitType unitType /*= TimeUnitType::milliSeconds*/)
145 {
146  return (std::clock() - cpuTimeKeeper) * cpuTimerConstants[(int)unitType];
147 }
148 
149 void
151 {
152  m_timer->reset();
153  m_accumulatedTimer = 0.;
154  m_ups = 0;
155  m_updateCount = 0;
156 }
157 
158 void
160 {
161  m_accumulatedTimer += m_timer->getTimeElapsed(StopWatch::TimeUnitType::milliSeconds);
162  m_updateCount++;
163 
164  if (m_accumulatedTimer > 1000.)
165  {
166  m_ups = m_updateCount;
167  m_updateCount = 0;
168  m_accumulatedTimer = 0.;
169  }
170 }
171 } // namespace imstk
double getTimeElapsed(const TimeUnitType unitType=TimeUnitType::milliSeconds) override
Returns the CPU time elapsed since calling start NOTE: This measurement is not entirely accurate espe...
Definition: imstkTimer.cpp:144
double getTotalLapTimes()
Returns the total time from all.
Definition: imstkTimer.cpp:50
virtual void start()
Start the appropriate timer.
Definition: imstkTimer.cpp:29
Compound Geometry.
void printTimeElapsed(std::string const &name=std::string("noName"), const TimeUnitType unitType=TimeUnitType::milliSeconds)
Print the elapsed time.
Definition: imstkTimer.cpp:108
void storeLap(std::string const &lapName)
Start the appropriate timer.
Definition: imstkTimer.cpp:61
void reset()
Reset the variable that keep track of ups.
Definition: imstkTimer.cpp:150
virtual void stop()
Start the appropriate timer.
Definition: imstkTimer.cpp:36
static std::string getTimeAndDate()
Returns a string with current date and time in UTC.
Definition: imstkTimer.cpp:101
void printLapTimes()
Print all the lap times.
Definition: imstkTimer.cpp:91
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
void setEndPointOfUpdate()
Set the end point to the update.
Definition: imstkTimer.cpp:159