MSDN Documentation

Windows API Reference

GetTickCount

DWORD GetTickCount(void);

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. This function is identical to GetTickCount64, except that GetTickCount has a 32-bit return value.

Return Value

The return value is the number of milliseconds that have elapsed since the system was started. This value is a 32-bit unsigned integer. Because the value wraps around every 49.7 days, you cannot use it to calculate the age of the system. However, it is suitable for identifying the approximate time of an event. For example, you can use it to determine how long a process has been running.

Remarks

The return value is a 32-bit unsigned integer. Because the value wraps around every 49.7 days, you cannot use it to calculate the age of the system. However, it is suitable for identifying the approximate time of an event. For example, you can use it to determine how long a process has been running.

Note that GetTickCount does not provide a way to measure elapsed time in a multithreaded or multiprocessor environment, as it returns the number of milliseconds since system start. For such scenarios, consider using QueryPerformanceCounter or a time-based function that is aware of elapsed time. If you need a value that does not wrap around, use GetTickCount64.

The tick count is based on the system's clock, so if the system clock is changed, the tick count will also change. This means that you cannot use the tick count to measure time intervals that may span across system clock adjustments.

Example

The following code example demonstrates how to use GetTickCount to measure the elapsed time of a particular operation:

#include <windows.h>
#include <iostream>

int main() {
    DWORD startTime = GetTickCount();

    // Simulate some work
    Sleep(1500); // Sleep for 1.5 seconds

    DWORD endTime = GetTickCount();
    DWORD elapsedTime = endTime - startTime;

    std::cout << "Operation took approximately " << elapsedTime << " milliseconds." << std::endl;

    return 0;
}

Requirements

Characteristic Details
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Header Winuser.h (include Windows.h)
Library User32.lib
DLL User32.dll

See Also