Last active
October 7, 2020 21:46
-
-
Save Lima-X/5d70dc9c863ba61290cb6676f5e24843 to your computer and use it in GitHub Desktop.
Windows High-Precision and light Benchmark Class Implementation using PerformanceCounters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
class Benchmark { | |
public: | |
enum class Resolution { | |
SEC = 1, | |
MILLI = 1000, | |
MICRO = 1000000, | |
NANO = 1000000000 | |
}; | |
Benchmark( | |
_In_ Resolution res = Resolution::MILLI | |
) | |
: m_res(res) | |
{ | |
if (!m_liFrequenzy.QuadPart) | |
QueryPerformanceFrequency(&m_liFrequenzy); | |
} | |
void Begin() { | |
QueryPerformanceCounter(&m_liBegin); | |
} | |
uin64_t End() { | |
QueryPerformanceCounter(&m_liEnd); | |
// Calculate time difference, Whole/Part is to avoid integer overflows | |
m_liEnd.QuadPart -= m_liBegin.QuadPart; | |
uin64_t Whole = (m_liEnd.QuadPart / m_liFrequenzy.QuadPart) * (uint64)m_res; | |
uin64_t Part = (m_liEnd.QuadPart % m_liFrequenzy.QuadPart) * (uint64)m_res; | |
Part /= m_liFrequenzy.QuadPart; | |
return Whole + Part; | |
} | |
private: | |
static LARGE_INTEGER m_liFrequenzy; | |
const Resolution m_res; | |
LARGE_INTEGER m_liBegin; | |
LARGE_INTEGER m_liEnd; | |
}; | |
LARGE_INTEGER Benchmark::m_liFrequenzy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment