From 68d39c708f7f773ca95c2d0cbed35fd1dfa093a2 Mon Sep 17 00:00:00 2001 From: Davis King Date: Fri, 15 Sep 2017 21:58:03 -0400 Subject: [PATCH] Changed TIME_THIS() to use std::chrono::high_resolution_clock --- dlib/time_this.h | 81 ++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 61 deletions(-) diff --git a/dlib/time_this.h b/dlib/time_this.h index 2db135062..09755c07e 100644 --- a/dlib/time_this.h +++ b/dlib/time_this.h @@ -4,74 +4,33 @@ #define DLIB_TIME_THIs_ -#include "platform.h" +#include - - -#ifndef WIN32 - -#include -#include -#include -#include // ---------------------------------------------------------------------------------------- -#define TIME_THIS_TO(_tt_op,_tt_out) \ - { \ - clock_t _tt_start, _tt_end; \ - tms _tt_timesbuf; \ - _tt_start = times(&_tt_timesbuf); \ - _tt_op; \ - _tt_end = times(&_tt_timesbuf); \ - long _tt_ticks = sysconf(_SC_CLK_TCK); \ - if ((double)(_tt_end-_tt_start)/(double)_tt_ticks < 1) \ - { \ - _tt_out << "\ntime: " \ - << (int)(1000*((double)(_tt_end-_tt_start)/(double)_tt_ticks)) << "ms\n"; \ - } \ - else \ - { \ - _tt_out << "\ntime: " \ - << (double)(_tt_end-_tt_start)/(double)_tt_ticks << "sec\n"; \ - } \ - } \ - +#define TIME_THIS_TO(_tt_op,_tt_out) \ + { \ + auto _tt_start = std::chrono::high_resolution_clock::now(); \ + {_tt_op;} \ + auto _tt_stop = std::chrono::high_resolution_clock::now(); \ + auto _tt_thetime = _tt_stop-_tt_start; \ + using std::chrono::duration_cast; \ + using std::chrono::duration; \ + if (_tt_thetime >= std::chrono::minutes(1)) \ + _tt_out << "\ntime: " << duration_cast>(_tt_thetime).count() << "min\n"; \ + else if (_tt_thetime >= std::chrono::seconds(1)) \ + _tt_out << "\ntime: " << duration_cast>(_tt_thetime).count() << "sec\n"; \ + else if (_tt_thetime >= std::chrono::milliseconds(1)) \ + _tt_out << "\ntime: " << duration_cast>(_tt_thetime).count() << "ms\n"; \ + else if (_tt_thetime >= std::chrono::microseconds(1)) \ + _tt_out << "\ntime: " << duration_cast>(_tt_thetime).count() << "us\n"; \ + else \ + _tt_out << "\ntime: " << duration_cast>(_tt_thetime).count() << "ns\n"; \ + } #define TIME_THIS(_tt_op) TIME_THIS_TO(_tt_op,std::cout) // ---------------------------------------------------------------------------------------- - -#endif - -#ifdef WIN32 - -#include "windows_magic.h" -#include // for GetTickCount() -#include - -// ---------------------------------------------------------------------------------------- - -#define TIME_THIS_TO(_tt_op,_tt_out) \ - { \ - unsigned long _tt_count = GetTickCount(); \ - _tt_op; \ - _tt_count = GetTickCount() - _tt_count; \ - if (_tt_count < 1000) \ - { \ - _tt_out << "\ntime: " << _tt_count << "ms\n"; \ - } \ - else \ - { \ - _tt_out << "\ntime: " << static_cast(_tt_count)/1000 << "sec\n"; \ - } \ - } \ - -#define TIME_THIS(_tt_op) TIME_THIS_TO(_tt_op,std::cout) - -// ---------------------------------------------------------------------------------------- - -#endif - #endif // DLIB_TIME_THIs_