CPU consumption in games

Started by
22 comments, last by CrazyCdn 16 years, 9 months ago
Hi all! I've been testing my game on different machine, and i noticed that the cpu consumption was different from machine to machine. Why do some machine get 100% cpu while other have 50% and some other have 12~20% ?? I tried playing with my main loop and by testing on every machine here is my code

MSG msg;
while(!done)
{
	ZeroMemory (&msg, sizeof(MSG));

	while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);

	        if (msg.message == WM_QUIT) return 0;
	}


	return 0;
}
If there's anything wrong, or unsafe way of doing things, please tell me. I tried adding a Sleep(1) before the ZeroMemory, it caused the consumption to go down quite a bit, but on 1 of the computer, i had lag when keyboard or mouse was in use, like if a WM_MESSAGE would make it lag. Thanks for any lights on this
Advertisement
Quote:
Why do some machine get 100% cpu

Because on those systems, there was only one core, and it was utilized all the time.

Quote:
while other have 50%

Because there were two cores, and one of them was utilized all the time.

Quote:
and some other have 12~20% ??

That one is trickier. Could be related to vsync preventing these systems from running at full speed.
Or it could be a slower GPU holding back the CPU?
Quote:Original post by Spoonbender
Quote:
Why do some machine get 100% cpu

Because on those systems, there was only one core, and it was utilized all the time.


Some of them are dual core machines. Some of them only gets to 100% cpu after a certain time playing

edit : are you saying that for a single core cpu, it is normal to have 100% cpu usage in a 3d rendered game ?

[Edited by - md_lasalle on June 21, 2007 7:09:18 PM]
If you are using Windows Task manager, the amount of percentage displayed
is not correct for each process. (Its distributed among processes--I forget
the exact details here)

If you want to make Task Manager happy, just add sleep(1)
to the end of your main animation loop.
A few things, drop the use of ZeroMemory(). There's no point its just wasted cycles.

DON'T Sleep(1), it can actually go up to 5ms. If you want to Sleep for one (1) ms then first use timeBeginPeriod(1) and restore it after with timeEndPeriod(1). The increases the accuracy of Sleep() and timeGetTime() to 1ms from I believe a default 10ms on 2000/XP/Vista. I put the timeBeginPeriod() before I enter my loop and restore it after (when the games shutting down and time accuracies don't matter.

I use Sleep(0) which just frees up the rest of your time slice. It's polite if you have spare cycles.

Oh and as a side note, if you're game is full screen don't worry about how much CPU you use. Heck I don't care even if I'm in a windowed mode with ours. They choose to start my application :) I am polite it might look like 100% usage but its really not.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Quote:Original post by md_lasalle
Quote:Original post by Spoonbender
Quote:
Why do some machine get 100% cpu

Because on those systems, there was only one core, and it was utilized all the time.


Some of them are dual core machines. Some of them only gets to 100% cpu after a certain time playing

edit : are you saying that for a single core cpu, it is normal to have 100% cpu usage in a 3d rendered game ?


It's perfectly normal.. and probably preferred. Whichever operating system you have dishes out the amount of CPU usage evenly to each application as needed. (To prove this, you can make an ifinite loop, and it won't freeze the rest of the computer because the OS will still give the other processes the time they need to run). HOWEVER, this is when priorities come in, if you set your priority of your application higher than 'normal', then the OS will give it more CPU time, freezing up the rest of your computer.

So, 100% CPU time is normal for games/other 3D apps, just don't mess with the process priorities.

~zix

EDIT: Also get process explorer. It gives the CPU usage out to two decimal places, so you may be a little clearer about what's using what.
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
Quote:Original post by Mike2343
A few things, drop the use of ZeroMemory(). There's no point its just wasted cycles.

DON'T Sleep(1), it can actually go up to 5ms. If you want to Sleep for one (1) ms then first use timeBeginPeriod(1) and restore it after with timeEndPeriod(1). The increases the accuracy of Sleep() and timeGetTime() to 1ms from I believe a default 10ms on 2000/XP/Vista. I put the timeBeginPeriod() before I enter my loop and restore it after (when the games shutting down and time accuracies don't matter.

I use Sleep(0) which just frees up the rest of your time slice. It's polite if you have spare cycles.

Oh and as a side note, if you're game is full screen don't worry about how much CPU you use. Heck I don't care even if I'm in a windowed mode with ours. They choose to start my application :) I am polite it might look like 100% usage but its really not.


thanks for the good suggestion, im playing with that right now, and working perfectly on my laptop

I agree with you on the fact that i should not care about my fullscreen game, but the thing is, with todays laptop, people do play on the go, so if i can reduce cpu usage from 100% to 50%, its better against heat, battery power etc

can you be more precise on where you use Sleep(0) in your code ? i tried it and doesnt seem to give the cpu a break, while Sleep(1) does

oh and btw, putting vsync on helps a lot for cpu loads... but i do all my tests with vsync of to measure the difference.
Sleep(0) doesn't give the CPU a break. It just allows other processes to run, if necessary. If you want to rest the CPU then you need a value of 1 or above. Even that isn't guaranteed to rest the CPU if you have other processes running to take advantage of that free time, but there's little you can do about that.
Quote:Original post by Kylotan
Sleep(0) doesn't give the CPU a break. It just allows other processes to run, if necessary. If you want to rest the CPU then you need a value of 1 or above. Even that isn't guaranteed to rest the CPU if you have other processes running to take advantage of that free time, but there's little you can do about that.


Indeed, Sleep(0) basically gives control back to the OS kernel and gives the kernel's process scheduler a chance to figure out what process is next in line for execution.
If you process is the only one in line it gets CPU control back immediately, thus not freeing up the CPU. If there is another process waiting to get CPU time that one will get control.
Doing a Sleep() with a value of 1 or up will put your process in a suspended mode, and it is guaranteed not to receive CPU control until the next time the scheduler checks if it can give a process CPU time. Mike2343 says this is 5 milliseconds, and that could very well be true.

There is one *possible* negative effect by doing a Sleep(0), and that is when a second process is consuming a lot of CPU power your own process might not get any CPU time for a "significant" time. I think that would be 25 milliseconds at least on Windows, the length of a "timeslice".
However, Mike2343 is right in saying it is "polite" to give up CPU control whenever you can. Thereby it is the user's responsibility to close any heavy applications.

Modern desktop OSes use a concept called timeslicing and if you want to learn more about process scheduling you should read up on that topic.
STOP THE PLANET!! I WANT TO GET OFF!!
Indeed if you use the timeBeginTime(1) stuff you should have a lot more accurate Sleep() function. So if you use 1 it will likely be 1 ~ 2ms. I place Sleep(0) at the bottom of my main loop. I also have a check if we have extra cycles (time) and if the application doesn't want to do extra AI or something gives it to the OS in a Sleep(1).

The only time Sleep(1) can be up to 5ms (sometimes more!) is when you haven't set timeBeginTime() or the next process is hogging of course.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

This topic is closed to new replies.

Advertisement