Microsecond resolution timestamps on Windows

后端 未结 8 593
日久生厌
日久生厌 2020-11-27 16:48

How do I get microsecond resolution timestamps on Windows?

I am loking for something better than QueryPerformanceCounter and QueryPerformanceFrequ

8条回答
  •  鱼传尺愫
    2020-11-27 17:26

    1. Windows is not a real-time OS.

    2. Processes on a multitasking OS will need to yield its time to another thread/process. This gives some overhead for timing.

    3. Every function call will have overhead thus giving a little delay when returning the request.

    4. Furthermore, the calling system call will need your process to switch from user space mode to kernel space mode, which has relatively high latency. You can overcome this by running the entire process in kernel mode (such as device driver code).

    5. Some OSes, like Linux or BSD, are better, but they still can not maintain accurate timing resolution to sub-microsecond (for example, the accuracy of nanosleep() on Linux is about 1 ms, not less than 1 ms), except you patch the kernel to some specific scheduler that give your application benefits.

    So I think, it's better to adapt your application to follow those issues, such as by recalibrating your timing routine often, which is what your links provide. AFAIK, the highest timer resolution for Windows is still GetPerformanceCounter/Frequency() regardless of its accuracy. You can get better accuracy by running you timer pooling routine inside a separate thread, and set that thread affinity to one core processor, and set the thread priority the highest you can get.

提交回复
热议问题