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