Poni Poni - 1 year ago 83
C Question

Fastest timing resolution system

What is the fastest timing system a C/C++ programmer can use?

For example:

time() will give the seconds since Jan 01 1970 00:00.

GetTickCount() on Windows will give the time, in milliseconds, since the system's start-up time, but is limited to 49.7 days (after that it simply wraps back to zero).

I want to get the current time, or ticks since system/app start-up time, in milliseconds.

The biggest concern is the method's overhead - I need the lightest one, because I'm about to call it many many times per second.

My case is that I have a worker thread, and to that worker thread I post pending jobs. Each job has an "execution time". So, I don't care if the time is the current "real" time or the time since the system's uptime - it just must be linear and light.


unsigned __int64 GetTickCountEx()
static DWORD dwWraps = 0;
static DWORD dwLast = 0;

DWORD dwCurrent = 0;


dwCurrent = GetTickCount();
if(dwLast > dwCurrent)

dwLast = dwCurrent;

unsigned __int64 timeResult = ((unsigned __int64)0xFFFFFFFF * dwWraps) + dwCurrent;


return timeResult;

Answer Source

For timing, the current Microsoft recommendation is to use QueryPerformanceCounter & QueryPerformanceFrequency.

This will give you better-than-millisecond timing. If the system doesn't support a high-resolution timer, then it will default to milliseconds (the same as GetTickCount).

Here is a short Microsoft article with examples of why you should use it :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download