Accuracy on Sleep function

Sleep function or accuracy can be achieved in the 1ms-2ms.

Very simple, to get the current time prior to Sleep, and then get at the current time after Sleep, compare two time difference to know.

E.g


SYSTEMTIME tBeforeSleep;
GetSystemTime(&tBeforeSleep);
Sleep(2);
SYSTEMTIME tAfterSleep;
GetSystemTime(&tAfterSleep);


Found that the time between the two either identical, or to a difference of about 15 milliseconds, even though I only Sleep 2 milliseconds. Also using the GetTickCount () function to get the same result found in milliseconds from start to the current time to test.


So I think even before Sleep (1), will be like Sleep (15) almost, might Sleep accuracy only to about 15 milliseconds, I recently discovered that this is wrong.


When Sleep (2), and the sleep time before the sleep or to the same, or to a difference of 10 milliseconds, because the accuracy is a function of acquisition time of 10 milliseconds, not Sleep () function problems.



In multimedia applications, there is a more accurate time function DWORD timeGetTime (), usage () is similar with GetTickCount, get the number of milliseconds to turn the current time. MSDN says that the function can be accurate to 5ms or more, depending on the speed of the machine. Further, with timeBeginPeriod and timeEndPeriod accuracy of the system provided function of time, so timeGetTime () has a higher degree of accuracy.

timeBeginPeriod(1);//设置系统精确到1秒
DWORD time1 = timeGetTime();
SYSTEMTIME sysTime1;
GetSystemTime(&sysTime1);
timeEndPeriod(1);//每个timeBeginPeriod对应一个timeEndPeriod取消提高精确度
 
Sleep(5);
	
timeBeginPeriod(1);
DWORD time2 = timeGetTime();
SYSTEMTIME sysTime2;
GetSystemTime(&sysTime2);
timeEndPeriod(1);

It was found that, Sleep function is quite accurate. timeGetTime function also,

However, the acquisition time is generally a function of the accuracy of GetSystemTime GetLocalTime GetTickCount the like is generally 10 milliseconds.

Published 15 original articles · won praise 2 · views 50000 +

Guess you like

Origin blog.csdn.net/imhikaru/article/details/7268312