2012-07-17 82 views
0

我使用OpenGL,Qt,C++編寫了3D模型顯示程序,但是我發現了一些奇怪的東西,即發佈模式版本中的FPS(每秒幀數)低於調試模式版本。現在我張貼他們的FPS:FPS:發佈模式版本低於調試

左邊是調試模式版本,右邊是發行模式版本:

enter image description here enter image description here

我用它來計算FPS功能是

void displayFPS() 
{ 
    static float framesPerSecond = 0.0f;  // This will store our fps 
    static float lastTime = 0.0f;  // This will hold the time from the last frame 
    float currentTime = GetTickCount() * 0.001f;  
    ++framesPerSecond; 
    if(currentTime - lastTime > 1.0f) 
    { 
     framesPerSecond/=currentTime - lastTime; 
     char strFrameRate[256]; 
     lastTime = currentTime; 
     sprintf_s(strFrameRate,256, "FPS : %f", framesPerSecond); 
     cout << strFrameRate << endl; 
     framesPerSecond = 0; 
    } 
} 

我不知道這怎麼會發生?不應該釋放模式比調試模式更快嗎?有人可以告訴我爲什麼嗎?

+0

我覺得很難從給出的信息中分辨出來。 displayFPS()函數應該可以正常工作。 btw ..爲什麼你使用sprintf_s寫入fps到控制檯?而不是'cout <<「FPS:」<< framesPerSecond << endl;' – Dirk 2012-07-18 07:58:05

+0

有時,vsync會導致有趣的效果。你有沒有嘗試禁用vsync?對於Nvidia GPU,您可以在Nvidia控制面板 - >管理3D設置 - >垂直同步:強制關閉。 – kroneml 2012-07-18 12:51:13

回答

1

根據this的準確性GetTickCount()比一毫秒差得多。它甚至可能像55毫秒一樣糟糕!使用更可靠的方法來測量時間間隔,如下所示:

#include <windows.h> 
#include <cstdint> 

typedef std::int64_t int64; 

// get duration of a single "clock" in microseconds 
double 
get_clock_duration() 
{ 
    LARGE_INTEGER f; 
    QueryPerformanceFrequency(&f); 
    return 1000000.0/double(f.QuadPart); 
} 

// get number of elapsed clocks from program start 
int64 
clocks() 
{ 
    LARGE_INTEGER t; 
    QueryPerformanceCounter(&t); 
    return t.QuadPart; 
} 

// convert a duration in clocks to a duration in milliseconds 
double 
milliseconds(int64 t) 
{ 
    return get_clock_duration() * double(t) * 0.001; 
}