Sign in to follow this  
schupf

Application faster after minimizing?

Recommended Posts

schupf    221
Hello,

I have a very strange behaviour in my application (a single Win32 render window).
I have a simple game loop and one frame lasts about 0,4 ms (I render almost nothing).
When I minimize/iconize the window and restore it, my loop seems to run faster! After the minimizing/restoring each frame
lasts about 0,16 ms.

Strange. Anyone encountered such a phenomenon or has an idea what could cause this? Or is this an indicator that my code has a bug in the timing?

Share this post


Link to post
Share on other sites
schupf    221
[quote name='ApochPiQ' timestamp='1310771955' post='4835830']
What functions are you using for timing purposes?
[/quote]
I am using QueryPerformanceCounter() to measure the elapsed time.
This is basically my render loop:
[code]
while(mIsAppRunning) {

if(mIsAppActive) {
deltaTime = mTimer.getElapsedTimeSec(); // uses QueryPerformanceCounter()
mAccumTime += deltaTime;

computeFps();

WindowUtilities::messagePump();
notifyInputHandler(deltaTime);

RS_STATE rsState = mRenderSystem->getState();
if(rsState == RS_READY) { // We only render if the RS is ready (e.g. not lost if we have a D3D9 RS)
renderOneFrame();
mFpsCounter++;
}
else if(rsState == RS_FATAL_ERROR)
exitApplicationLoop();
}
else
WindowUtilities::messagePump();

}
[/code]
Nothing special. I just measure the passed time since the last frame and add it to mAccumTime , call MessagePump() (which calls PeekMessage()) and render.

computeFps() computes the frames per second:
[code]
void Root::computeFps() {
if(mAccumTime > 1.0) {
mFps = static_cast<uint>(mFpsCounter / mAccumTime);

char c[200];
sprintf_s(c, "avg frame time: %8.5lf, fps: %i\n", val, mFps);
OutputDebugString(c);

int passedSeconds = static_cast<int>(mAccumTime);
mAccumTime = mAccumTime - passedSeconds;
mFpsCounter = mFpsCounter - passedSeconds * mFps;
}
}
[/code]
If 1 second is passed Icompute the fps, subtract passedSeconds from the accumulated time (for example if mAccumTime is 1.032, I subtract 1.0
and new mAccumTime is 0.032).
If I print the frames I get this result:
[quote]
avg frame time: 0.00050, fps: 2009
avg frame time: 0.00045, fps: 2224
avg frame time: 0.00045, fps: 2241
avg frame time: 0.00045, fps: 2240
avg frame time: 0.00046, fps: 2155
avg frame time: 0.00046, fps: 2195
DEACTIVATE
ACTIVATE
avg frame time: 0.00318, fps: 0
avg frame time: 0.00019, fps: 323
avg frame time: 0.00017, fps: 6049
avg frame time: 0.00015, fps: 6475
avg frame time: 0.00015, fps: 6478
avg frame time: 0.00016, fps: 6293
[/quote]
As you can see the first 6 seconds I have an average frame time of 0.00045 seconds. Then I minimized the window (which also pauses the timer), restored the window (timer resumed) and after the restoration I have average frame times of 0.00015 seconds! How is this possible?

Share this post


Link to post
Share on other sites
smasherprog    568
The code you have shown indicates that computeFps() should not be executed when the application mIsAppActive = false
My guess would be that youe Timer.getElapsedTimeSec returns the time that has passed since the last time the function was called, meaning if the function was not called for 5 seconds, it will return 5 seconds on the next call.
So, after the application is activated, your computefps function is screwed up because mAccumeTime now has 5 seconds in it

Run the fps calculation each frame instead of turning it off.

Share this post


Link to post
Share on other sites
wqking    761
Another guess, maybe your game doesn't really render to the window after the window minimized?
You may do a test by prevent the game from rendering to screen, but still render to back buffer if there is any, then test again.

Share this post


Link to post
Share on other sites
schupf    221
[quote name='smasherprog' timestamp='1310787474' post='4835885']
The code you have shown indicates that computeFps() should not be executed when the application mIsAppActive = false
My guess would be that youe Timer.getElapsedTimeSec returns the time that has passed since the last time the function was called, meaning if the function was not called for 5 seconds, it will return 5 seconds on the next call.
So, after the application is activated, your computefps function is screwed up because mAccumeTime now has 5 seconds in it
[/quote]
This never happened, because when the application was inactive/iconized I stopped the timer.

I extremly simplified my render loop for test purposes: I do NOTHING if the app is minimized/restored and compute the fps EVERY frame. But I still got the strange frame boost after the window is restored after minimizing!
When I start the app I have around 2500fps (avg frame time of 0.00048s). When I minimize the app, the fps are still around 2500 fps. But when I restore the minimized window, the fps skyrocket to 6000fps (avg frame time 0.00016s).

This is my whole simplified loop:
[code]
while(mIsAppRunning) {
deltaTime = mTimer.getElapsedTimeSec();
mTimes.push_back(deltaTime); // mTimes is a vector<double> that stores the delta times for one frame
mAccumTime += deltaTime;
computeFps();

WindowUtilities::messagePump();
renderOneFrame();
mFpsCounter++;
}

void Root::computeFps() {
if(mAccumTime > 1.0) {
mFps = static_cast<uint>((double)mFpsCounter / mAccumTime);

double sum = 0;
for(int i=0; i < mTimes.size(); i++)
sum+= mTimes[i];
double avgFrameTime = sum / mTimes.size();

char c[200];
sprintf_s(c, "avg frame time: %8.5lf, fps: %i\n", avgFrameTime, mFps);
OutputDebugString(c);
mTimes.clear();

int passedSeconds = static_cast<int>(mAccumTime);
mAccumTime = mAccumTime - passedSeconds;
mFpsCounter = mFpsCounter - passedSeconds * mFps;
}
}
[/code]
The loop does not get much simpler than that. I do absolutely NOTHING if the app is minimized/restored (except one OutputDebugString()).
This is my output:
[quote]avg frame time: 0.00044, fps: 2279
avg frame time: 0.00040, fps: 2496
avg frame time: 0.00040, fps: 2511
avg frame time: 0.00040, fps: 2472
avg frame time: 0.00040, fps: 2477
avg frame time: 0.00041, fps: 2449
DEACTIVATE // At this point I minimized the window. I keep rendering and the fps stay constant
avg frame time: 0.00044, fps: 2269
avg frame time: 0.00035, fps: 2885
avg frame time: 0.00034, fps: 2948
avg frame time: 0.00034, fps: 2933
avg frame time: 0.00034, fps: 2945
avg frame time: 0.00035, fps: 2849
avg frame time: 0.00035, fps: 2823
avg frame time: 0.00035, fps: 2870
ACTIVATE // This is the time where I restore the minimized window. The fps skyrocket.
avg frame time: 0.00026, fps: 3838
avg frame time: 0.00016, fps: 6363
avg frame time: 0.00015, fps: 6492
avg frame time: 0.00016, fps: 6207[/quote]

Anyone has an idea what could cause this? ;(

Share this post


Link to post
Share on other sites
schupf    221
[quote name='mhagain' timestamp='1310820545' post='4835972']
Why not double-check with something like FRAPS? That will tell you pretty quickly if the problem is in your timer/FPS counter or if it's weird driver behaviour.
[/quote]
Thats a good idea. I just made the test with fraps and fraps shows the same fps like my computation!
I made 2 screenshots:
Before the minimization of the window:
http://imageshack.us/f/6/beforeminimization.jpg/
(both my computation as well as fraps show about 2500 fps)

After the minimized window has been restored:
http://imageshack.us/photo/my-images/641/afterminimization.png/
(both my calculation and fraps say the fps is around 5400)

Seriously, what the hell is this?

Share this post


Link to post
Share on other sites
schupf    221
[quote name='rip-off' timestamp='1310827790' post='4836005']
Your window is grey, are you actually drawing anything? Measuring the time taken to do nothing is often meaningless.
[/quote]
The color is LightSlateGray (very close to gray;). Yes, I am rendering something (A teapot) - you just can't see it on the screenshots.

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