• Advertisement
Sign in to follow this  

How to prevent game from taking 99% CPU?

This topic is 4184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! Just a basic question, how do I prevent my game from taking 99% CPU while running the game WHILE loop. I tried SDL_Delay(); anywhere from 1-5 milliseconds, it works but then I dont get the smooth feeling (less FPS!) at all :( Please help :)

Share this post


Link to post
Share on other sites
Advertisement
Sure your game uses 100% CPU, because you ask it to generate as much frames per seconds as possible. That is no problem, since the game will probably the only thing that the user runs anyway, and he wants it to run as smooth as possible.

Share this post


Link to post
Share on other sites
As DaBono pointed out, you can't generate as many frames as possible and at the same time not allow the application to use as much resources as possible.
When you Sleep, or Delay as SDL calls it, you usually have a target update rate you wish to limit your application to. At the end of each frame you check to see how long the frame took, if it took less time than your target then you sleep the remaining time.

Share this post


Link to post
Share on other sites
Most monitors use the 60 hz of refrash rate (in windows, you can see it in control panel -> Display -> Display -> Settings -> Advanced). So it doesn´t matter if your application is able to show more than 60 hz frames per second, your screen refresh rate is updated just in 60 hz. So, if want to top limit your application refresh to 60 hz, you have to measure the time spent in the main loop if it is less than 1/60 seconds you sleep the rest.

Share this post


Link to post
Share on other sites
Perhaps I am getting confused here, but surely if I wrote two programs that both ran infinite while loops without sleeping, then I ran them both at the same time, wouldn't Windows decide how to portion up CPU time to each for me?

I thought Sleep() was just a way to offer CPU time to Windows. It'll take it anyway if it wants it, surely?

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
Perhaps I am getting confused here, but surely if I wrote two programs that both ran infinite while loops without sleeping, then I ran them both at the same time, wouldn't Windows decide how to portion up CPU time to each for me?

I thought Sleep() was just a way to offer CPU time to Windows. It'll take it anyway if it wants it, surely?


That's correct. Sleep just lets the OS know the app doesn't need the CPU time, but not sleeping doesn't mean the OS wont take it anyway.

Share this post


Link to post
Share on other sites
You shouldn't say that most monitors use 60 mHz vertical refresh rate. Many use 60,70, 72, 75, 85, etc. You would need to query the os to what refresh rate is being used first.

Share this post


Link to post
Share on other sites
The desired refresh needed to prevent flicker increases as screen size increases. It orignially deemed to be 60 Hz at 14", at 19" it is > 65 Hz.

This is compounded by the fact that AC outlets and flouresent lights operate at 60 Hz (in the US) so any monitor at 60 Hz is going to have flicker problems detectable to a certain percentage of the populous. The recommended refresh rates are approx. 75 Hz at < 21" and 85 Hz at > 21".

This is distinct from the idea of the human eye creating the facade of "continous motion" at a given framerate. Movies orignal thought that 18 was enough, but a large percentage of do not convert 18 frame into continous motion, so they changed to 24. Video people often use 30, for conversion reasons, but there has never been show any study suggesting the 24 frame movie is not close enough to ideal to be acceptable as perfect for 99% of the audience.

"twitch" games on the other hand want higher frame rates not to preserve the illusion of continuity, but to be information to the user with the lowest possible latency. Because latency is additive - the latency of you network, the game engine frame, the rendering, and the waiting for retrace all add up ... so if you want shooting to be accurate to a certain degree (such as 1/20th sec) you need latency below that, and as a general rule of thumb, twice as many frames per second to over the desired delay as a maximum instead of an average (in which case the maximum would be double the desired rate).

Personally I feel that a single player game maintaining 24-30 FPS is near perfect (if implemented right, some game have an extra 1 frame latency for using input, which feels weird). For multiplayer game I notice my accuracy of control and respone increases up to about 45 FPS when using my monitor at 85 Hz, and all the way up to 60 FPS when using 60 Hz (because missing a frame at 60 Hz effectivly means you are at 30 FPS for that frame).

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattrap
You would need to query the os to what refresh rate is being used first.


How do you do that under Windows?

Share this post


Link to post
Share on other sites
Just try out sleeping for 0 ms, this gives the OS the chance to check if there are any other processes currently needing some time or else go back to your program. Usually the CPU load goes also down ...

At least for windowed programs this makes sense, if you have a fullscreen game, why not use 100% of the CPU power like others mentioned before? Thats how games work ^^

Share this post


Link to post
Share on other sites
Quote:
Original post by abnormal
Just try out sleeping for 0 ms, this gives the OS the chance to check if there are any other processes currently needing some time or else go back to your program. Usually the CPU load goes also down ...

Lemme respond to that with a quote from one of the above posts:
Quote:

That's correct. Sleep just lets the OS know the app doesn't need the CPU time, but not sleeping doesn't mean the OS wont take it anyway.

The OS doesn't need your permission to check whether other processes need execution time. It just takes what it needs, and distributes leftover time between the processes that are interested. So no, you dont need to call sleep, not ever for 0 ms.

Quote:

At least for windowed programs this makes sense, if you have a fullscreen game, why not use 100% of the CPU power like others mentioned before? Thats how games work ^^

They do the same in Windowed mode.

The only rule is to not take CPU time you don't need. If you have nothing to do, you should definitely call Sleep. If you do have something to do, then do it, and leave the OS to decide how much time you should actually get. If you get 99% CPU, it just means no one else wanted the time.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Quote:
That's correct. Sleep just lets the OS know the app doesn't need the CPU time, but not sleeping doesn't mean the OS wont take it anyway.

The OS doesn't need your permission to check whether other processes need execution time. It just takes what it needs, and distributes leftover time between the processes that are interested. So no, you dont need to call sleep, not ever for 0 ms.


msdn sleep link

Quote:
MSDN
Parameters

millisecondsTimeout
The number of milliseconds for which the thread is blocked. Specify zero (0) to indicate that this thread should be suspended to allow other waiting threads to execute. Specify Infinite to block the thread indefinitely.

Share this post


Link to post
Share on other sites
Things are fairly simple. Windows give each thread a timeslice. When a thread's timeslice ends, Windows takes the control off that thread and gives it to the next, regardless whether Sleep() was called or not. The whole thing goes on circularly(or by whatever method the OS uses for distributing time to its threads) for all threads. Whether you call Sleep() or not from your thread doesn't matter, all threads will get their slice. If, however, a thread calls Sleep(0), it gives up the remaining time from its timeslice to the OS, which gives it to a thread of equal priority.

For example, if a thread's timeslice is 200ns and you don't call Sleep(), your thread will be suspended after 200ns, and another one(if other threads exist) will be executed. If you call Sleep() at 100ns, your thread will be suspended at that time and give up the remaining 100ns. Nothing is forcing you to call Sleep(), those 200ns were given to you by the OS and you have the right to use them all. It's not like 16-bit Windows which had pseudo-multithreading and,IIRC, you had to call Yield() in order to allow another thread to execute.

Share this post


Link to post
Share on other sites
If you use the 'wait for vsync' parameter, your app will be suspended until the swapping of buffers can go on. It means, it will make your app sleep until it is ready to swap the screen buffers. Effectively making your game loop (assuming it is also the render loop) run at the screen refresh rate.

As other says, Sleep( 0 ) make your app give up the remaining of his timeslice. If you don't want to use vsync, that would be a good way to limit the framerate, let say to 100Hz. I think Id's Quake2 engine uses something like:

while( isGameGoingTooFast() )
{
Sleep( 0 );
}

Even with 'vsync on', if the game loop isn't going fast enough to reach the refresh rate of the screen, the CPU will be floored anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Quote:

That's correct. Sleep just lets the OS know the app doesn't need the CPU time, but not sleeping doesn't mean the OS wont take it anyway.

The OS doesn't need your permission to check whether other processes need execution time. It just takes what it needs, and distributes leftover time between the processes that are interested. So no, you dont need to call sleep, not ever for 0 ms.


Good thing I didn't say the OS needs your permission then. Sleep *does* let the OS know you're finished with your current timeslice since it implicitly yields.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement