Add c++11 chrono timer handling

This commit is contained in:
Benau 2018-08-23 14:34:56 +08:00
parent 7e7a23854f
commit abc93beedf
2 changed files with 26 additions and 7 deletions

View File

@ -82,15 +82,18 @@ void StkTime::getDate(int *day, int *month, int *year)
if(year) *year = now->tm_year + 1900; if(year) *year = now->tm_year + 1900;
} // getDate } // getDate
// ----------------------------------------------------------------------------
StkTime::ScopeProfiler::ScopeProfiler(const char* name) StkTime::ScopeProfiler::ScopeProfiler(const char* name)
{ {
Log::info("ScopeProfiler", "%s {\n", name); Log::info("ScopeProfiler", "%s {\n", name);
m_time = (float)getRealTime(); m_time = getRealTimeMs();
m_name = name; m_name = name;
} } // StkTime::ScopeProfiler::ScopeProfiler
// ----------------------------------------------------------------------------
StkTime::ScopeProfiler::~ScopeProfiler() StkTime::ScopeProfiler::~ScopeProfiler()
{ {
float f2 = (float)getRealTime(); uint64_t difference = getRealTimeMs() - m_time;
Log::info("ScopeProfiler", "} // took %f s (%s)\n", (f2 - m_time), m_name.c_str()); Log::info("ScopeProfiler", "} // took %d ms (%s)\n",
} (int)difference, m_name.c_str());
} // StkTime::ScopeProfiler::ScopeProfiler

View File

@ -21,8 +21,11 @@
#include "ITimer.h" #include "ITimer.h"
#include <chrono>
#include <stdexcept> #include <stdexcept>
#include "utils/types.hpp"
#ifdef WIN32 #ifdef WIN32
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
# include <windows.h> # include <windows.h>
@ -90,7 +93,20 @@ public:
* The value is a double precision floating point value in seconds. * The value is a double precision floating point value in seconds.
*/ */
static double getRealTime(long startAt=0); static double getRealTime(long startAt=0);
// ------------------------------------------------------------------------
/** Returns a time based since epoch.
* The value is a 64bit unsigned integer in milliseconds.
*/
static uint64_t getRealTimeMs()
{
auto now = std::chrono::system_clock::now();
auto now_ms =
std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto value =
std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
return value.count();
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** /**
* \brief Compare two different times. * \brief Compare two different times.
@ -135,7 +151,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
class ScopeProfiler class ScopeProfiler
{ {
float m_time; uint64_t m_time;
std::string m_name; std::string m_name;
public: public:
ScopeProfiler(const char* name); ScopeProfiler(const char* name);