Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've read about this topic on numerous discussion sites, but I can't seem to find a clear definitive (up-to-date) answer, and hopefully this will me some more insight:

I've read the excellent game loop time article on Fix your timestep!, but it dosn't take any consideration of the CPU usage, so it will run at 100%. My question then, is it BAD to run at 100% CPU in your game loop?

  • If no, why? for example what about gaming on battery-driven devices like laptops? what about computer temperatures/fans/etc, and other windows applications?
  • If yes, why dosn't the article mention it, because surely that would impact it significantly? what is the best way to fix this on desktops/PC - is there only the unreliable sleep()? How would it affect the game loop suggested by the article?
share|improve this question
1  
That article is not about a game loop, its about fixing the time step for your physics engine, in case you write a physics engine. –  Maik Semder Mar 22 '13 at 19:45
    
@MaikSemder It is about time steps for physics engines, but it could be used for a main game loop with a little tinkering. –  Polar Mar 23 '13 at 9:32
add comment

1 Answer 1

up vote 3 down vote accepted

Running at 100% CPU is not necessarily bad. On the other hand, it can be quite a significant problem. Basically, it depends on what you're running on and how you're running it. It would be best to view a couple of scenarios to show this:

Scenario 1

  • On a dedicated gaming machine, with only the game open

In this case, it is a very good idea to use as much as possible of the CPU for your game. It'll end up running faster, and it won't be stealing CPU from anything that needs it (the system processes will still get what they need, don't worry about that).

Scenario 2

  • On a computer also being used for word, the internet, a couple of Youtube videos (like in that Chrome ad), etc.

In this case, using all of the CPU for your game will cause the user to eject the disk, snap it in two (ignoring the fact that they could get a refund), chuck it in a fire, and then flame your forums. Joking aside, it is never a good idea to use all of the CPU if there's a likelihood that the user will be doing something else with their computer at the same time.

An important thing to consider as well is whether the target has a single or a multi-core computer. With a single-core computer, your game could take up all of the processing power, causing everything else to grind to a halt. On the other hand, with a multi-core computer, unless you're making full use of both cores, it should cause no problem - you'll be taking up one core, but there will be more for the other applications.

At the end of the day, it depends on these sort of issues as to whether using 99% of the CPU time is a good or a bad thing. Intrinsically, it isn't, but there can be cases where it is. Just remember that (on a computer, anyway - games consoles are a different species), it is often up to the machine to virtually allocate CPU time to different applications.

share|improve this answer
    
Another option is to Sleep if the game is not the focus or if it hasn't detected user input within a certain time threshold (for a fast moving type of game this could be quite low), otherwise take the CPU. –  Jimmy Shelter Mar 22 '13 at 15:02
    
I was thinking of adding a flag of sorts into the main loop, which if enabled would trigger a sleep(0). If I have a game loop like the one in the article, could I safely do that and assume everything would still run as expected, only with lower CPU usage? –  KaiserJohaan Mar 22 '13 at 15:31
    
@KaiserJohaan Why would you use a sleep(0)? The only time that would take up is the function call. On a side note, sleeping would be perfectly fine, as it would not affect the timer which is sorting out the frames. My only concern with is that there are two reasons why you're using all of the CPU: 1) is a complicated calculation (needs CPU), 2) is idling, in which case sleep away. Most 'hobby' game loops sleep the remainder of frames anyway, if there's nothing left to do. –  Polar Mar 22 '13 at 15:41
    
@KaiserJohaan Oops, I meant sleep the remainder of time in the frame, that was a massive typo :P –  Polar Mar 22 '13 at 16:56
    
Actually sleep(0) is perfectly fine, because sleep will tell the OS "I'm done for the duration of my time slice, let some other process do something, and schedule me for at LEAST this much time later". Which is also why hardly any game uses sleep in its main loop. There are no guarantees that the game will be allowed to run again soon, which can cause severe stuttering in the frame rate. –  Kevin van der Velden Mar 22 '13 at 19:08
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.