I've noticed that, every time a program relies on a while
loop to keep it open, it uses almost 100% of the CPU. Adding a delay of 20ms puts that number down to 0%.
The following code would max the CPU:
while(executing){
// Do some things
if(Quit) executing = 0;
}
But the next wouldn't:
while(executing){
// Do some things
if(Quit) executing = 0;
delayFunction(20); //20ms delay
}
Is this the right way to do it? If so, what would be considered an adequate delay?
Are there better ways to avoid overloading the CPU?
Performance analyst's number one answer is, "It depends." There are lots of factors to consider, some making voluntary yield()
or delay()
look good, some making them look bad.