Changed TIME_THIS() to use std::chrono::high_resolution_clock

This commit is contained in:
Davis King 2017-09-15 21:58:03 -04:00
parent 0f8b227e47
commit 68d39c708f
1 changed files with 20 additions and 61 deletions

View File

@ -4,74 +4,33 @@
#define DLIB_TIME_THIs_ #define DLIB_TIME_THIs_
#include "platform.h" #include <chrono>
#ifndef WIN32
#include <sys/times.h>
#include <limits.h>
#include <unistd.h>
#include <iostream>
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
#define TIME_THIS_TO(_tt_op,_tt_out) \ #define TIME_THIS_TO(_tt_op,_tt_out) \
{ \ { \
clock_t _tt_start, _tt_end; \ auto _tt_start = std::chrono::high_resolution_clock::now(); \
tms _tt_timesbuf; \ {_tt_op;} \
_tt_start = times(&_tt_timesbuf); \ auto _tt_stop = std::chrono::high_resolution_clock::now(); \
_tt_op; \ auto _tt_thetime = _tt_stop-_tt_start; \
_tt_end = times(&_tt_timesbuf); \ using std::chrono::duration_cast; \
long _tt_ticks = sysconf(_SC_CLK_TCK); \ using std::chrono::duration; \
if ((double)(_tt_end-_tt_start)/(double)_tt_ticks < 1) \ if (_tt_thetime >= std::chrono::minutes(1)) \
{ \ _tt_out << "\ntime: " << duration_cast<duration<double>>(_tt_thetime).count() << "min\n"; \
_tt_out << "\ntime: " \ else if (_tt_thetime >= std::chrono::seconds(1)) \
<< (int)(1000*((double)(_tt_end-_tt_start)/(double)_tt_ticks)) << "ms\n"; \ _tt_out << "\ntime: " << duration_cast<duration<double>>(_tt_thetime).count() << "sec\n"; \
} \ else if (_tt_thetime >= std::chrono::milliseconds(1)) \
else \ _tt_out << "\ntime: " << duration_cast<duration<double,std::milli>>(_tt_thetime).count() << "ms\n"; \
{ \ else if (_tt_thetime >= std::chrono::microseconds(1)) \
_tt_out << "\ntime: " \ _tt_out << "\ntime: " << duration_cast<duration<double,std::micro>>(_tt_thetime).count() << "us\n"; \
<< (double)(_tt_end-_tt_start)/(double)_tt_ticks << "sec\n"; \ else \
} \ _tt_out << "\ntime: " << duration_cast<duration<double,std::nano>>(_tt_thetime).count() << "ns\n"; \
} \ }
#define TIME_THIS(_tt_op) TIME_THIS_TO(_tt_op,std::cout) #define TIME_THIS(_tt_op) TIME_THIS_TO(_tt_op,std::cout)
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
#endif
#ifdef WIN32
#include "windows_magic.h"
#include <windows.h> // for GetTickCount()
#include <iostream>
// ----------------------------------------------------------------------------------------
#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<double>(_tt_count)/1000 << "sec\n"; \
} \
} \
#define TIME_THIS(_tt_op) TIME_THIS_TO(_tt_op,std::cout)
// ----------------------------------------------------------------------------------------
#endif
#endif // DLIB_TIME_THIs_ #endif // DLIB_TIME_THIs_