[Common] Add high res time stamp
This commit is contained in:
parent
583d057a4e
commit
8036bdcd73
|
@ -0,0 +1,58 @@
|
|||
#include "stdafx.h"
|
||||
#include "HighResTimeStamp.h"
|
||||
#ifndef _WIN32
|
||||
#include <math.h>
|
||||
#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
|
||||
}
|
|
@ -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;
|
||||
};
|
Loading…
Reference in New Issue