Why does an empty loop use so much processor time?
c, c++
Solution
With the former, the condition `true` must be checked by the processor as often as the application can possibly get focus. As soon as the application gets processor attention, it checks `true`, just like any other loop condition, to see if the next instruction in the loop can be executed. Of course, the next instruction in the loop is also `true`. Basically, you are forcing the processor to constantly determine whether or not `true` is true, which, although trivial, when performed constantly bogs the processor down.
In the latter, the processor checks `true` once, and then executes the next statement. In this case, that is a 1ms wait. Since 1 ms is far greater than the amount of time required to check `true`, and the processor knows it can do other things during this wait, you free up a lot of power.
Problem
If I have an empty while loop in my code such as: ``` while(true); ``` It will drive the processor usage up to about 25%. However if I do the following: ``` while(true) Sleep(1); ``` It will only use about 1%. So why is that? Update: Thanks for all the great replies, but I guess I really should have asked this question, What's the algorithm behind sleep()? which is more of want I wanted to know.