Loop Design - Low CPU Cost
Loop Design - Low CPU Cost
Hey All,
I've been reading up on loop design for the last couple of days, searching forums and what not. Did some searching on this site to try and find out as much as possible before I start asking around on the forums.
As of right now, I'm using Sleep(1) to keep the CPU cost low on my "tight" loops. I have some GUI loops (for irrLicht) and some loops that use the typical "while (true){}" approach because they are waiting for flag variables to be set.
I'm curious to know what other folks are doing to keep their CPU usage down but still keep their program as efficient as possible. Any info or examples would be appreciated. Are there any cross-platform libs out there that work with handling system resources? I understand that handling system / CPU resources is OS dependent.
Thanks in advance for any info. Sorry if I missed a topic about this in the forum already.
I've been reading up on loop design for the last couple of days, searching forums and what not. Did some searching on this site to try and find out as much as possible before I start asking around on the forums.
As of right now, I'm using Sleep(1) to keep the CPU cost low on my "tight" loops. I have some GUI loops (for irrLicht) and some loops that use the typical "while (true){}" approach because they are waiting for flag variables to be set.
I'm curious to know what other folks are doing to keep their CPU usage down but still keep their program as efficient as possible. Any info or examples would be appreciated. Are there any cross-platform libs out there that work with handling system resources? I understand that handling system / CPU resources is OS dependent.
Thanks in advance for any info. Sorry if I missed a topic about this in the forum already.
-
- Posts: 157
- Joined: Tue Mar 20, 2007 8:30 am
I haven't had to worry about anything like this in my project yet, but I read about it somewhere a while back and it seemed pretty useful...
Basically, your main loop looks like this, not exactly like this, I know, but bear with me...
Now as you know, all that is happening every frame, which can sometimes give the CPU alot of things to do. What you can do though is change it around a little so that some things are only happeneing every second or third frame by doing something like this, again it's half C++, half pseudocode so yours would look diferent...
I hope thats not too hard to follow, but basically doSomething(); is running every frame, doSomethingElse(); is running every second frame and doSomeOtherStuff(); is running every third frame.
It might not seem like much written here, but think about how you would use it in a game. Less important things (like input and even AI and physics depending on your game) can be made to run every second or third frame, or even less often if you want, whereas more important things can be run every frame to make sure your game runs smoothly.
Well that was quite a post... Hope that helps
Basically, your main loop looks like this, not exactly like this, I know, but bear with me...
Code: Select all
while(device->run())
{
if (device->isWindowActive())
{
driver->beginScene(true, true, video::SColor(255,0,0,255));
doSomething();
doSomethingElse();
doSomeOtherStuff();
smgr->drawAll();
driver->endScene();
}
}
Code: Select all
int framecount = 0;
while(device->run())
{
if (device->isWindowActive())
{
driver->beginScene(true, true, video::SColor(255,0,0,255));
framecount++;
doSomething();
if (framecount == 2)
{
doSomethingElse();
}
if (framecount == 3)
{
doSomeOtherStuff();
framecount = 0;
}
smgr->drawAll();
driver->endScene();
}
}
It might not seem like much written here, but think about how you would use it in a game. Less important things (like input and even AI and physics depending on your game) can be made to run every second or third frame, or even less often if you want, whereas more important things can be run every frame to make sure your game runs smoothly.
Well that was quite a post... Hope that helps
Tell me what you cherish most. Give me the pleasure of taking it away.
Thanks a lot for the post and insights.olivehehe_03 wrote:Well that was quite a post... Hope that helps
What you put up there definitely wasn't confusing. Actually, you made me think more about what I'm trying to accomplish with my loops. I was so concerned with just trying to find a way to free up resources that I was missing the real objective: freeing up resources when actions are NOT being performed.
Sleep() is working really good (since I'm on windows - inactive program times are at 0% CPU now) but when I get to porting it over I'm going to have to add some defines for using a *nix based alternative (read some of them already during my research).
Thanks again for your post - it helped out a lot .
this isnt exactly what you need but it might help i gues http://www.mindcontrol.org/~hplus/graph ... _loop.html
Thanks - it's definitely some good info on loop design. I might have to incorporate something like that later.roxaz wrote:this isnt exactly what you need but it might help i gues http://www.mindcontrol.org/~hplus/graph ... _loop.html
Well that will definitely help me with portability . I'll be testing that one out tonight.Luden wrote:"- Added sleep() methd to IrrlichtDevice, for pausing the Irrlicht process for a longer amount of time."
Changelog for 1.3
I don't know a whole lot about loop design. I just have one loop in which everything happens:
The go(time) tells the various components to do their stuff, and lets them know how much time has passed (so how far a character should be moved, etc.)
Code: Select all
ITimer* timer = device->getTimer();
u32 timelast = timer->getRealTime()-1;
u32 timenow = timer->getRealTime();
// Main loop
while (device->run())
{
timenow = timer->getRealTime();
time = timenow - timelast;
timelast = timer->getRealTime();
// Start the scene
video->beginScene(true, true, SColor(255, 0, 0, 0));
if (time > 0) {
control->go(time);
level->go(time);
}
// Draw everything
scene->drawAll();
gui->drawAll();
// End the scene
video->endScene();
}
Well, my problem stems from having some network data "queues" in place that need to be continuously checked for new data. If there is no data, the loop basically comes down to "while(true){}" so it's burning a lot of CPU. I don't want the loop itself to be "paused" because I want the queue to be checked as frequently as possible.Vuen wrote:I don't know a whole lot about loop design. I just have one loop in which everything happens:
The go(time) tells the various components to do their stuff, and lets them know how much time has passed (so how far a character should be moved, etc.)Code: Select all
ITimer* timer = device->getTimer(); u32 timelast = timer->getRealTime()-1; u32 timenow = timer->getRealTime(); // Main loop while (device->run()) { timenow = timer->getRealTime(); time = timenow - timelast; timelast = timer->getRealTime(); // Start the scene video->beginScene(true, true, SColor(255, 0, 0, 0)); if (time > 0) { control->go(time); level->go(time); } // Draw everything scene->drawAll(); gui->drawAll(); // End the scene video->endScene(); }