Changed the timing code to use the C++11 high resolution clock and

atomics.  This makes the timing code a lot more precise.
pull/919/head
Davis King 7 years ago
parent 13442ec1b3
commit 895d7874d3

@ -3,7 +3,8 @@
#ifndef DLIB_TImING_Hh_ #ifndef DLIB_TImING_Hh_
#define DLIB_TImING_Hh_ #define DLIB_TImING_Hh_
#include "misc_api.h" #include <chrono>
#include <atomic>
#include <cstring> #include <cstring>
#include "string.h" #include "string.h"
@ -76,9 +77,9 @@ namespace dlib
const int TIME_SLOTS = 500; const int TIME_SLOTS = 500;
const int NAME_LENGTH = 40; const int NAME_LENGTH = 40;
inline uint64* time_buf() inline std::atomic<uint64_t>* time_buf()
{ {
static uint64 buf[TIME_SLOTS] = {0}; static std::atomic<uint64_t> buf[TIME_SLOTS];
return buf; return buf;
} }
@ -96,26 +97,26 @@ namespace dlib
return buf[i]; return buf[i];
} }
inline timestamper& ts() inline uint64_t ts()
{ {
static timestamper ts_; using namespace std::chrono;
return ts_; return duration_cast<duration<double,std::nano>>(high_resolution_clock::now().time_since_epoch()).count();
} }
inline void start(int i ) inline void start(int i)
{ {
time_buf()[i] -= ts().get_timestamp(); time_buf()[i] -= ts();
} }
inline void start(int i, const char* name) inline void start(int i, const char* name)
{ {
time_buf()[i] -= ts().get_timestamp(); time_buf()[i] -= ts();
name_buf(i,name); name_buf(i,name);
} }
inline void stop(int i) inline void stop(int i)
{ {
time_buf()[i] += ts().get_timestamp(); time_buf()[i] += ts();
} }
inline void print() inline void print()
@ -140,7 +141,7 @@ namespace dlib
{ {
if (time_buf()[i] != 0) if (time_buf()[i] != 0)
{ {
double time = time_buf()[i]/1000.0; double time = time_buf()[i]/1000.0/1000.0;
string name; string name;
// Check if the name buffer is empty. Use the name it contains if it isn't. // Check if the name buffer is empty. Use the name it contains if it isn't.
if (name_buf(i,"")[0] != '\0') if (name_buf(i,"")[0] != '\0')

Loading…
Cancel
Save