From 651c07c72430acb7d94efa45454dfa6c620ece7a Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Fri, 22 Aug 2014 00:50:48 +0200 Subject: [PATCH] Add global timer utils Global timers can be used to accumulate timings of the same context in a thread-safe way. --- Util/TimingUtil.h | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/Util/TimingUtil.h b/Util/TimingUtil.h index c1505cebf..a0cc67e0b 100644 --- a/Util/TimingUtil.h +++ b/Util/TimingUtil.h @@ -29,11 +29,51 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define TIMINGUTIL_H #include +#include +#include +#include + +struct GlobalTimer +{ + GlobalTimer() : time(0) {} + std::atomic time; +}; + +class GlobalTimerFactory +{ +public: + static GlobalTimerFactory& get() + { + static GlobalTimerFactory instance; + return instance; + } + + GlobalTimer& getGlobalTimer(const std::string& name) + { + std::lock_guard lock(map_mutex); + return timer_map[name]; + } + +private: + std::mutex map_mutex; + std::unordered_map 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(_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_STOP(_X) _X##_stop = std::chrono::steady_clock::now() -#define TIMER_MSEC(_X) std::chrono::duration_cast(_X##_stop - _X##_start).count() -#define TIMER_SEC(_X) (0.001*std::chrono::duration_cast(_X##_stop - _X##_start).count()) +#define TIMER_NSEC(_X) std::chrono::duration_cast(_X##_stop - _X##_start).count() +#define TIMER_USEC(_X) std::chrono::duration_cast(_X##_stop - _X##_start).count() +#define TIMER_MSEC(_X) (0.000001*std::chrono::duration_cast(_X##_stop - _X##_start).count()) +#define TIMER_SEC(_X) (0.000001*std::chrono::duration_cast(_X##_stop - _X##_start).count()) #define TIMER_MIN(_X) std::chrono::duration_cast(_X##_stop - _X##_start).count() #endif // TIMINGUTIL_H