Sign in to follow this  
Joyal

PeekMessage high cpu usage

Recommended Posts

Joyal    137

Hello,

 

Im working making a 2D game from scracth using Directx 11 , Windows 8.1 , and Visual Studios 2012

 

I create a simple window and show some sprites, but i realized that my "Game" is using a lot of CPU Usage.

After debuggin i spotted that PeekMessage was the one causing this, i even try to remove all the code and just leave a Blank Window and still using a lot of CPU without anything, it uses between 15-30 % of CPU , this is my game loop

        MSG msg;
        ZeroMemory( &msg, sizeof(msg) );

        long lastTime = GetTickCount();
        while( msg.message!=WM_QUIT)
        {

            if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }else{

                //Gametime Calc
                long deltaTime = GetTickCount() - lastTime;
                lastTime = GetTickCount();
            
                float gameTime = (float) deltaTime / 1000;

                Update(gameTime);
                Render(gameTime);
            }  
            //Sleep(1);
        }

you will notice that i have a commeted line "Sleep", if i add that line , the CPU usage go down to 0 - 0.7%  , but it drops the FPS count like 70%

 

i also tried remove everything

        MSG msg;
        ZeroMemory( &msg, sizeof(msg) );

        long lastTime = GetTickCount();
        while( msg.message!=WM_QUIT)
        {

            if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }else{


            }  
        }

 and even without the IF statment , or any other code, just the blank window, and still using cpu about 15-30 %

Share this post


Link to post
Share on other sites
Joyal    137

You are probably having a busy loop. Try to add a count to the loop to see how fast the loop runs.

 

i have an FPS counter, is around 2000 cicles per second

Share this post


Link to post
Share on other sites
fastcall22    10838
Is the question "why does my simple game take so much CPU"?

Most operating systems are capable of multitasking. In Windows, multitasking is performed by giving each running process or thread a time-slice (~20 ms in Windows) to execute. When the time-slice expires, the operating system preemptively takes control of the process or thread and the operating system selects another thread or process. A thread can also yield the rest of the time-slice back to the operating system via Sleep. With [tt]Sleep[/tt] your process or thread's execution is returned to the operating system and is rescheduled to execute at a later time of some multiple of a time-slice length. "CPU utilization" is estimated by calculating how much of each time-slice a process uses. Therefore, you can use 100% of your CPU just by [tt]while (true);[/tt], and you can use [tt]Sleep[/tt] to reduce "CPU usage" and forfeit the remaining time-slice. You may want to consider handling WM_ACTIVATE and conserve CPU when your game is out of focus, but use as much CPU as possible when your game does have focus.

Additionally, do not use a variable time step; read Gaffer's Fix Your Timestep article, and use a fixed time step to update your game.

Share this post


Link to post
Share on other sites
SeanMiddleditch    17565

So, you already know there is a busy loop. What's your question?

 
how to avoid that ?


Enable Vsync or add your sleep call back (maybe a more sophisticated one that calculates how long to sleep based on a target reasonable framerate). You have absolutely no need for 2000 FPS so don't worry that it will drop. You are rendering frames at a ridiculous rate your monitor can't even display.

Share this post


Link to post
Share on other sites
Joyal    137

After i read all your answer , it seems to me that the issue is because my "Game" is doing anything, apart of just showing some sprite, but not logic or anything, and because of that, the loop is being process very fast , so when the loop has more logic, this CPU usage should go down (ironically),  right? 

Share this post


Link to post
Share on other sites
Tom KQT    1704

After i read all your answer , it seems to me that the issue is because my "Game" is doing anything, apart of just showing some sprite, but not logic or anything, and because of that, the loop is being process very fast , so when the loop has more logic, this CPU usage should go down (ironically),  right? 

Nope. It really doesn't matter how much you are doing in the loop. An empty while (true); loop will take 100 % of one CPU core, exactly the same way as a very busy loop will.

The fact that you are not doing much in your code is the reason why you have so high FPS, not why you have so high CPU usage.

The only way how to limit your CPU usage is by adding Sleep, as others suggested.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this