Fastest timing resolution system

后端 未结 10 2234
死守一世寂寞
死守一世寂寞 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.

提交回复
热议问题