Fastest timing resolution system

后端 未结 10 2202
死守一世寂寞
死守一世寂寞 2020-12-03 06:11

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

相关标签:
10条回答
  • 2020-12-03 06:47

    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.

    0 讨论(0)
  • 2020-12-03 06:51

    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 :)

    0 讨论(0)
  • 2020-12-03 06:53

    On Mac OS X, you can simple use UInt32 TickCount (void) to get the ticks.

    0 讨论(0)
  • 2020-12-03 06:58

    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...

    0 讨论(0)
提交回复
热议问题