Skip to content
Snippets Groups Projects
Commit be16205f authored by artivis's avatar artivis Committed by Jeremie Deray
Browse files

add timer

parent c5757dc4
No related branches found
No related tags found
1 merge request!90[WIP] ProcessorBase multi-threading
#ifndef WOLF_TIMER_H_
#define WOLF_TIMER_H_
#include <chrono>
namespace wolf
{
namespace details
{
using std::chrono::hours;
using std::chrono::minutes;
using std::chrono::seconds;
using std::chrono::milliseconds;
using std::chrono::microseconds;
using std::chrono::nanoseconds;
template<typename _Clock, typename _Dur>
using time_point = std::chrono::time_point<_Clock, _Dur>;
using wolf_time_unit = nanoseconds;
using wolf_clock_t = std::chrono::_V2::high_resolution_clock;
} // namespace details
/**
* @brief Timer. A tic-toc timer.
*
* Mesure the elapsed time between construction - or tic() -
* and toc(). The elapsed time is expressed in unit.
*
* @param unit. The time unit.
* @see unit
*/
template <typename unit>
class Timer
{
public:
/**
* @brief Timer. Launch the timer.
*/
Timer(): start_(now()) { }
/**
* @brief ~Timer. Default desctructor.
*/
~Timer() = default;
/**
* @brief tic. Reset the timer.
*/
void tic()
{
start_ = now();
}
/**
* @brief toc. Return this elapsed time since construction or last tic().
* @return double. The elapsed time.
* @see tic()
*/
template <typename T = int64_t>
T toc()
{
return static_cast<T>(cast_d(now() - start_).count());
}
protected:
details::time_point<details::wolf_clock_t, unit> start_;
template <typename... Args>
auto cast_d(Args&&... args) ->
decltype(std::chrono::duration_cast<unit>(std::forward<Args>(args)...))
{
return std::chrono::duration_cast<unit>(std::forward<Args>(args)...);
}
template <typename... Args>
auto cast(Args&&... args) ->
decltype(std::chrono::time_point_cast<unit>(std::forward<Args>(args)...))
{
return std::chrono::time_point_cast<unit>(std::forward<Args>(args)...);
}
auto now() ->
decltype(std::declval<Timer<unit>>().cast(details::wolf_clock_t::now()))
{
return cast(std::chrono::system_clock::now());
}
};
using timer_secs = Timer<details::seconds>;
using timer_msecs = Timer<details::milliseconds>;
using timer_usecs = Timer<details::microseconds>;
using timer_nsecs = Timer<details::microseconds>;
} // namespace wolf
#endif // WOLF_TIMER_H_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment