From 895d7874d3e9089396f64d1a528415ca8d0ed6ac Mon Sep 17 00:00:00 2001 From: Davis King Date: Wed, 1 Nov 2017 16:30:42 -0400 Subject: [PATCH] Changed the timing code to use the C++11 high resolution clock and atomics. This makes the timing code a lot more precise. --- dlib/timing.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/dlib/timing.h b/dlib/timing.h index 42c09e3ca..dcad2a3a6 100644 --- a/dlib/timing.h +++ b/dlib/timing.h @@ -3,7 +3,8 @@ #ifndef DLIB_TImING_Hh_ #define DLIB_TImING_Hh_ -#include "misc_api.h" +#include +#include #include #include "string.h" @@ -76,9 +77,9 @@ namespace dlib const int TIME_SLOTS = 500; const int NAME_LENGTH = 40; - inline uint64* time_buf() + inline std::atomic* time_buf() { - static uint64 buf[TIME_SLOTS] = {0}; + static std::atomic buf[TIME_SLOTS]; return buf; } @@ -96,26 +97,26 @@ namespace dlib return buf[i]; } - inline timestamper& ts() + inline uint64_t ts() { - static timestamper ts_; - return ts_; + using namespace std::chrono; + return duration_cast>(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) { - time_buf()[i] -= ts().get_timestamp(); + time_buf()[i] -= ts(); name_buf(i,name); } inline void stop(int i) { - time_buf()[i] += ts().get_timestamp(); + time_buf()[i] += ts(); } inline void print() @@ -140,7 +141,7 @@ namespace dlib { if (time_buf()[i] != 0) { - double time = time_buf()[i]/1000.0; + double time = time_buf()[i]/1000.0/1000.0; string name; // Check if the name buffer is empty. Use the name it contains if it isn't. if (name_buf(i,"")[0] != '\0')