In one of my programs, I have a while loop. A fellow programmer recommended to sleep for 1 ms at the end of the loop to be cpu friendly. My question : is there a way to sleep for less then 1 ms? No matter how complicated, I would like to know the answer. Thank you. What kind of a program is it? But if it's something like say an editor which spends most of it's time waiting for user input, then there should be no reason for it to be using any CPU time until the user does something.
Sleep only guarantees a minimum time, not an absolute time. Typically, sleep times get rounded up to the default scheduling interval of the operating system say 20mS. If you're doing lots of calculations, but don't want to cripple the machine, a better way would be to drop the priority of the process. This ensures that maximum effort is spent doing calculations so you're not waiting for the result , but also ensures that you retain responsive control of the machine should you need to do other things.
I wanted to sleep for less then 1 ms to boost performance not much but still, with huge amounts its noticeable. We can also sleep for 10 seconds without a problem. We will just provide the 10 to the sleep function like below.
As stated previously the sleep function will interpret given value as the second. What if we need to sleep in milliseconds which is lower than second. We can use decimal or float values. Any additional feedback?
Submit and view feedback for This product This page. View all page feedback. In this article. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. This behavior changed starting with Windows Server This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds.
The system clock "ticks" at a constant rate. If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on.
To increase the accuracy of the sleep interval, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum.
Use caution when calling timeBeginPeriod , as frequent calls can significantly affect the system clock, system power usage, and the scheduler.
0コメント