7 #include "imstkTimer.h" 8 #include "imstkLogger.h" 13 StopWatch::wcTimerConstants[] = {
17 1 / (1000.0 * 60.0 * 60)
21 CpuTimer::cpuTimerConstants[] = {
22 1000.0 / CLOCKS_PER_SEC,
24 1.0 / (CLOCKS_PER_SEC * 60.0),
25 1.0 / (CLOCKS_PER_SEC * 60.0 * 60.0)
31 wallClockTimeKeeper = std::chrono::high_resolution_clock::now();
32 this->state = TimerState::started;
39 this->state = TimerState::stopped;
46 this->state = TimerState::stopped;
52 double totalTime = 0.0;
53 for (
const auto& timeChunk : lapTimes)
55 totalTime += timeChunk;
61 StopWatch::storeLap(std::string
const& lapName)
63 if (this->lapTimes.size() > 0)
65 this->lapTimes.push_back(this->
getTimeElapsed() - *this->lapTimes.end());
72 if (lapName ==
"noName")
74 this->lapNames.push_back(
"Lap " + std::to_string(lapNames.size()));
78 this->lapNames.push_back(lapName);
81 this->state = TimerState::stopped;
87 this->
storeLap(std::string(
"noName"));
93 LOG(INFO) <<
"Lap times:";
94 for (
size_t i = 0; i < this->lapTimes.size(); ++i)
96 LOG(INFO) <<
"\t" << this->lapNames.at(i) <<
": " << this->lapTimes.at(i) <<
"ms";
103 time_t now = time(0);
104 return std::string(asctime(gmtime(&now)));
109 const TimeUnitType unitType )
113 case TimeUnitType::milliSeconds:
114 LOG(INFO) << name <<
": " << this->
getTimeElapsed(unitType) <<
" ms.";
117 case TimeUnitType::seconds:
118 LOG(INFO) << name <<
": " << this->
getTimeElapsed(unitType) <<
" sec.";
121 case TimeUnitType::minutes:
122 LOG(INFO) << name <<
": " << this->
getTimeElapsed(unitType) <<
" min.";
125 case TimeUnitType::hours:
126 LOG(INFO) << name <<
": " << this->
getTimeElapsed(unitType) <<
" hrs.";
130 LOG(WARNING) <<
"Type of the time integrator not identified!";
138 return std::chrono::duration<double, std::milli>
139 (std::chrono::high_resolution_clock::now() - wallClockTimeKeeper).count() *
140 wcTimerConstants[(int)unitType];
146 return (std::clock() - cpuTimeKeeper) * cpuTimerConstants[(int)unitType];
153 m_accumulatedTimer = 0.;
161 m_accumulatedTimer += m_timer->getTimeElapsed(StopWatch::TimeUnitType::milliSeconds);
164 if (m_accumulatedTimer > 1000.)
166 m_ups = m_updateCount;
168 m_accumulatedTimer = 0.;
double getTimeElapsed(const TimeUnitType unitType=TimeUnitType::milliSeconds) override
Returns the CPU time elapsed since calling start NOTE: This measurement is not entirely accurate espe...
double getTotalLapTimes()
Returns the total time from all.
virtual void start()
Start the appropriate timer.
void printTimeElapsed(std::string const &name=std::string("noName"), const TimeUnitType unitType=TimeUnitType::milliSeconds)
Print the elapsed time.
void storeLap(std::string const &lapName)
Start the appropriate timer.
void reset()
Reset the variable that keep track of ups.
virtual void stop()
Start the appropriate timer.
static std::string getTimeAndDate()
Returns a string with current date and time in UTC.
void printLapTimes()
Print all the lap times.
virtual double getTimeElapsed(const TimeUnitType unitType=TimeUnitType::milliSeconds)
Returns the time elapsed since calling start.
virtual void reset()
Clears all the laps.
void setEndPointOfUpdate()
Set the end point to the update.