Radium Engine  1.5.20
Loading...
Searching...
No Matches
Chronometer.hpp
1#pragma once
2
3#include <Core/Utils/Timer.hpp>
4
5namespace Ra {
6namespace Core {
7namespace Utils {
8
38class RA_CORE_API Chrono
39{
40 public:
41 using MicroSeconds = long;
42 using Seconds = Scalar;
46 Chrono() {}
47
51 Chrono( const Chrono& other ) = default;
52
56 Chrono( Chrono&& other ) = default;
57
62
70 template <class Function, class... Args>
71 inline void run( Function&& f, Args&&... args ) {
72 m_start = Clock::now();
73 f( args... );
74 m_end = Clock::now();
75 }
76
86 template <typename ReturnType, class Function, class... Args>
87 inline ReturnType run( Function&& f, Args... args ) {
88 // TODO //static_assert( /*check if ReturnType is equal to Function return type*/,
89 // "RETURN_TYPE_DO_NOT_MATCH_FUNCTION_RETURN_TYPE" );
90 m_start = Clock::now();
91 ReturnType res = f( args... );
92 m_end = Clock::now();
93 return res;
94 }
95
105 template <std::size_t Times, class Function, class... Args>
106 inline MicroSeconds test( Function&& f, Args&&... args ) {
107 MicroSeconds avg = 0;
108 for ( std::size_t i = 0; i < Times; ++i ) {
109 m_start = Clock::now();
110 f( args... );
111 m_end = Clock::now();
112 avg += getIntervalMicro( m_start, m_end );
113 }
114 avg /= Times;
115 m_start = Clock::now();
116 m_end = m_start + std::chrono::microseconds( avg );
117 return avg;
118 }
119
125 inline MicroSeconds elapsedMicroSeconds() const { return getIntervalMicro( m_start, m_end ); }
126
132 inline Seconds elapsedSeconds() const { return getIntervalSeconds( m_start, m_end ); }
133
137 inline Chrono& operator=( const Chrono& other ) = default;
138
142 inline Chrono& operator=( Chrono&& other ) = default;
143
147 inline bool operator==( const Chrono& other ) const {
148 return ( elapsedMicroSeconds() == other.elapsedMicroSeconds() );
149 }
150
154 inline bool operator<( const Chrono& other ) const {
155 return ( elapsedMicroSeconds() < other.elapsedMicroSeconds() );
156 }
157
158 protected:
162};
163
164} // namespace Utils
165} // namespace Core
166} // namespace Ra
The Chrono class represents a chronometer for timing generic functions in an easy way.
Chrono & operator=(const Chrono &other)=default
Copy assignment operator.
ReturnType run(Function &&f, Args... args)
Run the given ReturnType function f( args ... ) and times it.
bool operator<(const Chrono &other) const
Less operator.
TimePoint m_end
Time after running the function.
bool operator==(const Chrono &other) const
Equal operator.
MicroSeconds test(Function &&f, Args &&... args)
Run the given function f( args ... ) n Times and compute the average timing.
Chrono(const Chrono &other)=default
Copy constructor.
TimePoint m_start
VARIABLE.
MicroSeconds elapsedMicroSeconds() const
Return the elapsed time for last call of run in microseconds.
Seconds elapsedSeconds() const
Return the elapsed time for last call of run in seconds.
void run(Function &&f, Args &&... args)
Run the given void function f( args ... ) and times it.
Chrono(Chrono &&other)=default
Move constructor.
Chrono & operator=(Chrono &&other)=default
Move assignment operator.
Chrono()
Default constructor.
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3