PeekMessage high cpu usage

Started by
7 comments, last by TomKQT 10 years, 3 months ago

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 %

Advertisement

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

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

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

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

how to avoid that ?

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 Sleep 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 while (true);, and you can use Sleep 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.

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.

Sean Middleditch – Game Systems Engineer – Join my team!

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?

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.

This topic is closed to new replies.

Advertisement