diff --git a/Source/Common/HighResTimeStamp.cpp b/Source/Common/HighResTimeStamp.cpp new file mode 100644 index 000000000..29a59b130 --- /dev/null +++ b/Source/Common/HighResTimeStamp.cpp @@ -0,0 +1,58 @@ +#include "stdafx.h" +#include "HighResTimeStamp.h" +#ifndef _WIN32 +#include +#endif + +#ifdef _WIN32 +bool HighResTimeStamp::m_GotFreq = false; +uint64_t HighResTimeStamp::m_Freq = { 0 }; +#endif + +HighResTimeStamp::HighResTimeStamp() +{ +#ifndef _WIN32 + m_time = 0; +#else + if (!m_GotFreq) + { + LARGE_INTEGER value; + QueryPerformanceFrequency(&value); + m_Freq = value.QuadPart; + } + m_time = 0; +#endif +} + +HighResTimeStamp & HighResTimeStamp::SetToNow(void) +{ +#ifndef _WIN32 + struct timespec now; + memset(&now, 0, sizeof(now)); + clock_gettime(CLOCK_MONOTONIC, &now); + m_time = ((uint64_t)now.tv_sec * (uint64_t)1000000l) + (now.tv_nsec / 1000); +#else + LARGE_INTEGER value; + QueryPerformanceCounter(&value); + m_time = value.QuadPart; +#endif + return *this; +} + +uint64_t HighResTimeStamp::GetMicroSeconds(void) +{ +#ifndef _WIN32 + return m_time; +#else + return (m_time * 1000000) / m_Freq; +#endif +} + +void HighResTimeStamp::SetMicroSeconds(uint64_t MicroSeconds) +{ +#ifndef _WIN32 + m_time = MicroSeconds; +#else + m_time = (MicroSeconds * m_Freq) / 1000000; +#endif +} \ No newline at end of file diff --git a/Source/Common/HighResTimeStamp.h b/Source/Common/HighResTimeStamp.h new file mode 100644 index 000000000..74ef8152f --- /dev/null +++ b/Source/Common/HighResTimeStamp.h @@ -0,0 +1,18 @@ +#pragma once +#include "stdtypes.h" + +class HighResTimeStamp +{ +public: + HighResTimeStamp(); + HighResTimeStamp & SetToNow (void); + uint64_t GetMicroSeconds(void); + void SetMicroSeconds(uint64_t MicroSeconds); + +private: +#ifdef _WIN32 + static bool m_GotFreq; + static uint64_t m_Freq; +#endif + uint64_t m_time; +}; \ No newline at end of file