Fastest timing resolution system

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

    GetSystemTimeAsFileTime is the fastest resource. Its granularity can be obtained by a call to GetSystemTimeAdjustment which fills lpTimeIncrement. The system time as filetime has 100ns units and increments by TimeIncrement. TimeIncrement can vary and it depends on the setting of the multimedia timer interface.

    A call to timeGetDevCaps will disclose the capabilities of the time services. It returns a value wPeriodMin for the minimum supported interrupt period. A call to timeBeginPeriod with wPeriodMin as argument will setup the system to operate at highest possible interrupt frequency (typically ~1ms). This will also force the time increment of the system filetime returned by GetSystemTimeAsFileTime to be smaller. Its granularity will be in the range of 1ms (10000 100ns units).

    For your purpose, I'd suggest to go for this approach.

    The QueryPerformanceCounter choice is questionable since its frequency is not accurate by two means: Firstly it deviates from the value given by QueryPerformanceFrequency by a hardware specific offset. This offset can easely be several hundred ppm, which means that a conversion into time will contain an error of several hundreds of microseconds per second. Secondly it has thermal drift. The drift of such devices can easely be several ppm. This way another - heat dependend - error of several us/s is added.

    So as long as a resolution of ~1ms is sufficient and the main question is the overhead, GetSystemTimeAsFileTime is by far the best solution.

    When microseconds matter, you'd have to go a longer way and see more details. Sub-millisecond time services are described at the Windows Timestamp Project

提交回复
热议问题