High Resolution Timer

Windows provides a high resoltuion timer API which we can use to profile code and calculate the time between game frames, delta time. Before we can use this API, we need to call QueryPerformanceFrequency to check the frequency of the timer. This frequency is set once, at computer boot time, and will not change later. If we don't call QueryPerformanceFrequency first on the thread, QueryPerformanceCounter might fail.

With all that out of the way, let's create a variable to hold the timer frequency, a start and a stop counter. These variables are all 64 bit integers, windows defines these API's to use LARGE_INTEGER. Let's also create a variable for storing the delta time between start and stop, i'm going to make this a long long.

LARGE_INTEGER timerFrequency;
LARGE_INTEGER timerStart;
LARGE_INTEGER timerStop;
LONGLONG timerDelta;

Initializing the timer is straight forward, just call QueryPerformanceFrequency.

if (!QueryPerformanceFrequency(&timerFrequency)) {
    std::cout << "WinMain: QueryPerformanceFrequency failed\n";
}

Now to actually time some code. We can get the current system timer by calling QueryPerformanceCounter. We can get the delta between the two snapshots by subtracting the QuatPart of each LARGE_INTEGER

QueryPerformanceCounter(&timerStart);
//SomeCodeToProfile();
QueryPerformanceCounter(&timerStop);
timerDelta = timerStop.QuadPart - timerStart.QuadPart;

The unit of the high resolution timer, is ticks. You need to know the timer frequency (ticks per second) to convert it to a number. We know the number of ticks per second from which we do from QueryPerformanceFrequency. Sometimes you might want to convert the number from seconds to milliseconds to display.

double seconds = (double)timerDelta / (double)timerFrequency.QuadPart;
double milliseconds = (double)timerDelta * 1000.0 / (double)timerFrequency.QuadPart;

We can put all this together to find the delta time between frames, or to time some function.

LARGE_INTEGER timerFrequency;
LARGE_INTEGER timerStart;
LARGE_INTEGER timerStop;

void Update() {
    // End the timer from last frame
    QueryPerformanceCounter(&timerStop); 
    LONGLONG timerDelta = timerStop.QuadPart - timerStart.QuadPart;
    
    // Find delta time
    double deltaTime = (double)timerDelta / (double)timerFrequency.QuadPart;

    // Do some work

    // Start timing between frames
    QueryPerformanceCounter(&timerStart); 
}

GetTickCount

A high resolution timer is always the way to go, but windows offers an alternate timer trough GetTickCount. GetTickCount works in milliseconds, it returns the number of milliseconds since the computer has booted. This number rolls over every 49.7 days. A lot of what we do in games is on the millisecond level of accuracy, which makes this method of timing viable. But again, the high resolution timer is better.

DWORD lastTick = 0;
void Update() {
    DWORD thisTick = GetTickCount();
    float deltaTime = float(thisTick - lastTick) * 0.001f;
    lastTick = thisTick;
}