2012-11-14 23:39:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-25 22:14:10 +00:00
|
|
|
#include "types.h"
|
|
|
|
|
2013-11-19 10:30:58 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
2012-11-14 23:39:56 +00:00
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
private:
|
2014-02-16 15:19:06 +00:00
|
|
|
bool m_stopped;
|
2017-01-29 00:12:00 +00:00
|
|
|
std::chrono::steady_clock::time_point m_start;
|
|
|
|
std::chrono::steady_clock::time_point m_end;
|
2012-11-14 23:39:56 +00:00
|
|
|
|
|
|
|
public:
|
2014-02-16 15:19:06 +00:00
|
|
|
Timer() : m_stopped(false)
|
2012-11-14 23:39:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2014-02-16 15:19:06 +00:00
|
|
|
m_stopped = false;
|
2017-01-29 00:12:00 +00:00
|
|
|
m_start = std::chrono::steady_clock::now();
|
2012-11-14 23:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stop()
|
|
|
|
{
|
2014-02-16 15:19:06 +00:00
|
|
|
m_stopped = true;
|
2017-01-29 00:12:00 +00:00
|
|
|
m_end = std::chrono::steady_clock::now();
|
2012-11-14 23:39:56 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 15:19:06 +00:00
|
|
|
double GetElapsedTimeInSec() const
|
2012-11-14 23:39:56 +00:00
|
|
|
{
|
2015-02-18 21:23:31 +00:00
|
|
|
return double(GetElapsedTimeInMicroSec()) / 1000000.0;
|
2014-02-16 15:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double GetElapsedTimeInMilliSec() const
|
|
|
|
{
|
2015-02-18 21:23:31 +00:00
|
|
|
return double(GetElapsedTimeInMicroSec()) / 1000.0;
|
2014-02-16 15:19:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 21:23:31 +00:00
|
|
|
u64 GetElapsedTimeInMicroSec() const
|
2014-02-16 15:19:06 +00:00
|
|
|
{
|
2017-01-29 00:12:00 +00:00
|
|
|
std::chrono::steady_clock::time_point now = m_stopped ? m_end : std::chrono::steady_clock::now();
|
2014-02-16 15:19:06 +00:00
|
|
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(now - m_start).count();
|
|
|
|
}
|
|
|
|
|
2015-02-18 21:23:31 +00:00
|
|
|
u64 GetElapsedTimeInNanoSec() const
|
2014-02-16 15:19:06 +00:00
|
|
|
{
|
2017-01-29 00:12:00 +00:00
|
|
|
std::chrono::steady_clock::time_point now = m_stopped ? m_end : std::chrono::steady_clock::now();
|
2014-02-16 15:19:06 +00:00
|
|
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(now - m_start).count();
|
2012-11-14 23:39:56 +00:00
|
|
|
}
|
2018-10-02 18:25:36 +00:00
|
|
|
|
|
|
|
u64 GetMsSince(std::chrono::steady_clock::time_point timestamp)
|
|
|
|
{
|
|
|
|
std::chrono::steady_clock::time_point now = m_stopped ? m_end : std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(now - timestamp).count();
|
|
|
|
}
|
2013-11-19 10:30:58 +00:00
|
|
|
};
|