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 wi
If you're just worried about GetTickCount()
overflowing, then you can just wrap it like this:
DWORDLONG GetLongTickCount(void)
{
static DWORDLONG last_tick = 0;
DWORD tick = GetTickCount();
if (tick < (last_tick & 0xffffffff))
last_tick += 0x100000000;
last_tick = (last_tick & 0xffffffff00000000) | tick;
return last_tick;
}
If you want to call this from multiple threads you'll need to lock access to the last_tick
variable. As long as you call GetLongTickCount()
at least once every 49.7 days, it'll detect the overflow.
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 :)
On Mac OS X, you can simple use UInt32 TickCount (void) to get the ticks.
Have you reviewed the code in this MSDN article?
http://msdn.microsoft.com/en-us/magazine/cc163996.aspx
I have this code compiling on a Windows 7 64bit machine using both VC2005 and C++ Builder XE but when executing, it locks up my machine; have not debugged far enough to figure out why yet. It seems overly complicated. Templates of templates of templates of UG...