Skip to content

Instantly share code, notes, and snippets.

@Lima-X
Last active October 7, 2020 21:46
Show Gist options
  • Save Lima-X/5d70dc9c863ba61290cb6676f5e24843 to your computer and use it in GitHub Desktop.
Save Lima-X/5d70dc9c863ba61290cb6676f5e24843 to your computer and use it in GitHub Desktop.
Windows High-Precision and light Benchmark Class Implementation using PerformanceCounters
#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