11
\$\begingroup\$

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?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 22, 2013 at 19:45
  • \$\begingroup\$ @MaikSemder It is about time steps for physics engines, but it could be used for a main game loop with a little tinkering. \$\endgroup\$
    – Polar
    Commented Mar 23, 2013 at 9:32

1 Answer 1

6
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 22, 2013 at 15:02
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 22, 2013 at 15:31
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Polar
    Commented Mar 22, 2013 at 15:41
  • \$\begingroup\$ @KaiserJohaan Oops, I meant sleep the remainder of time in the frame, that was a massive typo :P \$\endgroup\$
    – Polar
    Commented Mar 22, 2013 at 16:56
  • 4
    \$\begingroup\$ 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. \$\endgroup\$
    – Elva
    Commented Mar 22, 2013 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .