可計時 納秒-小時
/************************************************************************************
**
** Copyright (C) 2020, Shaoguang. All rights reserved.
**
** Date : 2020/04/01
**
************************************************************************************/
#ifndef ELAPSED_TIMER_H_
#define ELAPSED_TIMER_H_
#include <chrono>
/** \class ElapsedTimer
\brief Defines an elapsed timer.
*/
template<class TimeT = double>
class ElapsedTimer
{
public:
using chrono_clock_type = std::chrono::high_resolution_clock;
using time_point = typename chrono_clock_type::time_point;
using time_t = TimeT;
/** Constructs a elapsed timer, and start timing. */
ElapsedTimer() : m_tp(chrono_clock_type::now()) {}
/** Restart timing. */
void reset()
{
m_tp = chrono_clock_type::now();
}
/** Returns the elapsed time in milliseconds. */
time_t elapsed() const
{
return elapsedMilliseconds();
}
/** Returns the elapsed time in nanoseconds. */
time_t elapsedNanoseconds() const
{
return static_cast<nanoseconds_t>(chrono_clock_type::now() - m_tp).count();
}
/** Returns the elapsed time in microseconds. */
time_t elapsedMicroseconds() const
{
return static_cast<microseconds_t>(chrono_clock_type::now() - m_tp).count();
}
/** Returns the elapsed time in milliseconds. */
time_t elapsedMilliseconds() const
{
return static_cast<milliseconds_t>(chrono_clock_type::now() - m_tp).count();
}
/** Returns the elapsed time in seconds. */
time_t elapsedSeconds() const
{
return static_cast<seconds_t>(chrono_clock_type::now() - m_tp).count();
}
/** Returns the elapsed time in minutes. */
time_t elapsedMinutes() const
{
return static_cast<minutes_t>(chrono_clock_type::now() - m_tp).count();
}
/** Returns the elapsed time in hours. */
time_t elapsedHours() const
{
return static_cast<hours_t>(chrono_clock_type::now() - m_tp).count();
}
private:
using nanoseconds_t = std::chrono::duration<time_t, std::chrono::nanoseconds::period>;
using microseconds_t = std::chrono::duration<time_t, std::chrono::microseconds::period>;
using milliseconds_t = std::chrono::duration<time_t, std::chrono::milliseconds::period>;
using seconds_t = std::chrono::duration<time_t, std::chrono::seconds::period>;
using minutes_t = std::chrono::duration<time_t, std::chrono::minutes::period>;
using hours_t = std::chrono::duration<time_t, std::chrono::hours::period>;
time_point m_tp;
};
#endif // ELAPSED_TIMER_H_