Add global timer utils
Global timers can be used to accumulate timings of the same context in a thread-safe way.
This commit is contained in:
parent
4d27b75897
commit
651c07c724
@ -29,11 +29,51 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#define TIMINGUTIL_H
|
#define TIMINGUTIL_H
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <atomic>
|
||||||
|
#include <mutex>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
struct GlobalTimer
|
||||||
|
{
|
||||||
|
GlobalTimer() : time(0) {}
|
||||||
|
std::atomic<long unsigned> time;
|
||||||
|
};
|
||||||
|
|
||||||
|
class GlobalTimerFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static GlobalTimerFactory& get()
|
||||||
|
{
|
||||||
|
static GlobalTimerFactory instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
GlobalTimer& getGlobalTimer(const std::string& name)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(map_mutex);
|
||||||
|
return timer_map[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::mutex map_mutex;
|
||||||
|
std::unordered_map<std::string, GlobalTimer> timer_map;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLOBAL_TIMER_AQUIRE(_X) auto& _X##_global_timer = GlobalTimerFactory::get().getGlobalTimer(#_X)
|
||||||
|
#define GLOBAL_TIMER_RESET(_X) _X##_global_timer.time = 0
|
||||||
|
#define GLOBAL_TIMER_START(_X) TIMER_START(_X)
|
||||||
|
#define GLOBAL_TIMER_STOP(_X) TIMER_STOP(_X); _X##_global_timer.time += TIMER_NSEC(_X)
|
||||||
|
#define GLOBAL_TIMER_NSEC(_X) static_cast<double>(_X##_global_timer.time)
|
||||||
|
#define GLOBAL_TIMER_USEC(_X) (_X##_global_timer.time / 1000.0)
|
||||||
|
#define GLOBAL_TIMER_MSEC(_X) (_X##_global_timer.time / 1000.0 / 1000.0)
|
||||||
|
#define GLOBAL_TIMER_SEC(_X) (_X##_global_timer.time / 1000.0 / 1000.0 / 1000.0)
|
||||||
|
|
||||||
#define TIMER_START(_X) auto _X##_start = std::chrono::steady_clock::now(), _X##_stop = _X##_start
|
#define TIMER_START(_X) auto _X##_start = std::chrono::steady_clock::now(), _X##_stop = _X##_start
|
||||||
#define TIMER_STOP(_X) _X##_stop = std::chrono::steady_clock::now()
|
#define TIMER_STOP(_X) _X##_stop = std::chrono::steady_clock::now()
|
||||||
#define TIMER_MSEC(_X) std::chrono::duration_cast<std::chrono::milliseconds>(_X##_stop - _X##_start).count()
|
#define TIMER_NSEC(_X) std::chrono::duration_cast<std::chrono::nanoseconds>(_X##_stop - _X##_start).count()
|
||||||
#define TIMER_SEC(_X) (0.001*std::chrono::duration_cast<std::chrono::milliseconds>(_X##_stop - _X##_start).count())
|
#define TIMER_USEC(_X) std::chrono::duration_cast<std::chrono::microseconds>(_X##_stop - _X##_start).count()
|
||||||
|
#define TIMER_MSEC(_X) (0.000001*std::chrono::duration_cast<std::chrono::nanoseconds>(_X##_stop - _X##_start).count())
|
||||||
|
#define TIMER_SEC(_X) (0.000001*std::chrono::duration_cast<std::chrono::microseconds>(_X##_stop - _X##_start).count())
|
||||||
#define TIMER_MIN(_X) std::chrono::duration_cast<std::chrono::minutes>(_X##_stop - _X##_start).count()
|
#define TIMER_MIN(_X) std::chrono::duration_cast<std::chrono::minutes>(_X##_stop - _X##_start).count()
|
||||||
|
|
||||||
#endif // TIMINGUTIL_H
|
#endif // TIMINGUTIL_H
|
||||||
|
Loading…
Reference in New Issue
Block a user