Jump to content
  • Advertisement
Sign in to follow this  
Alpha Nox

Game Perfomance

This topic is 4828 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, I'm programming a 2d game like zelda style, and every frame it draws the whole screen again,it refreshes it. I'm using SDL, but I the CPU USAGE during the game is 80-90% of the CPU. Is it too high? Is there a way lower it? Will it be able to play on slower machines? My computer processor: Atlhon 2600+ Thanks, Victor Freire

Share this post


Link to post
Share on other sites
Advertisement
Most likely you're only using that much CPU because you're game isn't giving any time back to the OS to do other things. Try putting a sleep command in your main loop of just a millisecond and see if your CPU usage takes a drastic drop without killing your framerate.

I run into this problem when I have multiple threads going in tight loops. I little sleep does wonders.

Share this post


Link to post
Share on other sites
For games, this is normal. A game is a real-time application (as opposed to event driven) so it should always be using the CPU. If this concerns you, you could try putting some sleep calls into your main game loop to give time to other processes, but generally this need not be done.

Share this post


Link to post
Share on other sites
I'm using a 10ms delay when the frame rendering is over and it keeps that way 80-90%.

Thanks for the help but will it run in lower-end machines?

Thanks again,

Share this post


Link to post
Share on other sites
Most games have nearly 100% CPU usage, no matter what system they are running on. This is not because they are inefficient, exactly, it is because they generally don't include any Sleep() (or equivalent) calls which would give the processor a chance to idle if there was excess processing power. Instead, they typically just render more frames per second if they are running better. CPU usage is pretty irrelevant, unless you expect the user to be trying to do something else while playing your game. What matters is the frame rate you achieve. Shoot for about 60 frames a second, and if you hit at least 25 frames a second on your target minimum system requirements, you are good to go. Once the game starts running faster than about 30 frames a second, it all blurs together for the eye and there is no apparent difference between 35 frames a second and 50 (except maybe if your physics engine is locked to the same frame rate as the renderer, in which case 50 frames a second may look a bit more realistic). Thus, anything less than 30 frames a second will jar the eye, but anything more than about 60 is indistinguishable from a frame rate in the ideal range, and thus a waste of effort to optimize for.

Share this post


Link to post
Share on other sites
Ok, thanks for anwsering quickly

I increased the FPS limiter a bit and put a 10ms delay after the frame rendering.

I'm getting a 70-80% CPU usage and the OS keeps running fine with the game running now.

Thanks for your help everyone,

Share this post


Link to post
Share on other sites
Another solution is to pause your game whenever it does not have the focus. This will allow you to perform general OS tasks while still keeping the game window up.

Share this post


Link to post
Share on other sites
*sigh* here we go again.
No, it's not a problem that your game uses 99% CPU.

First, doesn't this stall the rest of my computer?
No it doesn't.
Windows isn't entirely stupid. It takes all the processes that are ready to run (aren't waiting for a timer, or incoming network data or some other event), and it splits the available time between them.

So, let's say your computer has 10 processes running. That means each process would get 10%. However, 9 of them are just background processes that does a tiny bit of processing, and then yields the CPU again. Examples are Winamp, which might need to load a bit more data into its buffer, or maybe process a few milliseconds of audio, or your ftp client, your firewall, which just has to look up whether an incoming packet should be blocked, or your antivirus scanner which just has to check if you've done anything lately which requires it to kick into action.
So none of them actually need more than a few microseconds of CPU time. After that, they call Sleep() to give the CPU away to whoever else wants it.
So, when your game has spent its 10% allocated CPU time (because your game doesn't call Sleep, it just takes what it can get), Windows looks around, realizes that the 9 other processes refused to use any more CPU time. That leaves, say, 89% (if the 9 first processes used 1% cpu time combined) to the only process which is willing to use it. Your game. Add that to the time it was already allocated, and it ends up spending 99%. Not because it's greedy and steals the CPU from the rest of the system, but because no other process needs the CPU.

Try quitting your game and call up the task manager. The Idle process usually uses 99% of the CPU. That means the CPU is doing nothing 99% of the time. So why shouldn't your game take the CPU time it's offered? If you make your game call Sleep, and give away the CPU, then that will just go to the Idle process, which does nothing with it. How is that an improvement?
(And yes, I know I simplified all the above quite a bit)
But the bottom line is that your program only gets its fair share + the leftovers that no one wants. That is simply how Windows, or any other half decent OS works. No matter what you do, you simply can't steal CPU time that another process has been given.

Second, How come my game uses that much CPU? Does that mean it can't run on my CPU, or that I'm a lousy programmer?
Again, no. Just look at your code. You have a main loop, which basically, takes input, checks Windows messages, renders a frame.
After that, what happens? It does it all again! It doesn't stop for a quick coffee break or to phone its friends. When it finishes one loop iteration, it hurries on to do it again. So why *shouldn't* this end up taking 100% of the CPU? You're not telling it to take a break, you're telling it to run this loop, and that's what it does. As fast as possible.
It doesn't mean you're a sloppy coder. Basically, any program will always take 100% of the CPU as long as it's running. The only exception is that the OS can overrule it when neccesary, to give time to other processes, or to itself.
But if you leave a program to itself, it uses 100% CPU until it ends.

So don't bother "fixing" it with calls to Sleep or anything else. It's not a problem that needs fixing (and the "cure" would mean lower responsiveness, since the game might be sleeping when the user tries to press a key or click the mouse. Not a good idea in a game)

Of course, as skittleo said, make sure to pause the game when it doesn't have focus. When the game isn't even being played, it should obviously make sure not to take more time than it absolutely needs.

Oh, and sorry if I sound grumpy. Wasn't really my intention. [wink]

[Edited by - Spoonbender on July 29, 2005 4:00:40 PM]

Share this post


Link to post
Share on other sites
Just like you, Alpha Nox, I was quite stunned to see my first SDL project take up so many CPU-time, especially since I thought not much was going on and I had a (preliminary) frame limit of 40 fps.

After some searching on the Internet I found out that my game was taking a huge perfomance hit from the fact that I used images with an alpha channel. The blending made sure nothing could be hardware accelerated using standard SDL. After switching to glSDL (which makes use of your 3D-hardware) my usage dropped from 30 to a mere 5%.
So, if you are using many alpha-blended images and are not willing to give up on the neat-looking graphics, try looking into glSDL and friends..

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!