I rarely use C on Windows for real projects, and this time it bit me.I had the following test program. I wanted to do a simple library test. Sleep() is said to provide millisecond-level delays, so I naturally used 1 ms to drive it.Cwhile(1) { MBx_Ticks(1000); // 换算为微秒传入MBx驱动 TestMemUpdate(1); // 毫秒传入测试函数 Sleep(1); } That was, of course, completely inaccurate — as unreliable as Python. You can add the following code to measure the actual delay.C#include <windows.h> #include <time.h> SYSTEMTIME t1; while(1) { MBx_Ticks(1000); // 换算为微秒传入MBx驱动 TestMemUpdate(1); // 毫秒传入测试函数 Sleep(1); /* 打印毫秒时间戳 */ GetSystemTime(&t1); printf("%ld\n", (int)t1.wMilliseconds); } Here is a short excerpt. You can see it's far from 1 ms, but for testing it doesn't matter.SH276 291 307 323 339 354 370 385
Precision of the Sleep() Function in Windows C
I rarely use C on Windows for real projects, and this time it bit me.
I had the following test program. I wanted to do a simple library test. Sleep() is said to provide millisecond-level delays, so I naturally used 1 ms to drive it.
That was, of course, completely inaccurate — as unreliable as Python. You can add the following code to measure the actual delay.
Here is a short excerpt. You can see it's far from 1 ms, but for testing it doesn't matter.