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_
#include "platform.h"
#ifndef WIN32
#include <sys/times.h>
#include <limits.h>
#include <unistd.h>
#include <iostream>
// ----------------------------------------------------------------------------------------
#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(_tt_op) TIME_THIS_TO(_tt_op,std::cout)
// ----------------------------------------------------------------------------------------
#endif
#ifdef WIN32
#include "windows_magic.h"
#include <windows.h> // for GetTickCount()
#include <iostream>
#include <chrono>
// ----------------------------------------------------------------------------------------
#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"; \
} \
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<duration<double>>(_tt_thetime).count() << "min\n"; \
else if (_tt_thetime >= std::chrono::seconds(1)) \
_tt_out << "\ntime: " << duration_cast<duration<double>>(_tt_thetime).count() << "sec\n"; \
else if (_tt_thetime >= std::chrono::milliseconds(1)) \
_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: " << duration_cast<duration<double,std::micro>>(_tt_thetime).count() << "us\n"; \
else \
{ \
_tt_out << "\ntime: " << static_cast<double>(_tt_count)/1000 << "sec\n"; \
} \
} \
_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)
// ----------------------------------------------------------------------------------------
#endif
#endif // DLIB_TIME_THIs_