ゲームの最後の2〜4秒のFPSを計算したいと思います。これを行うための最良の方法は何でしょうか?
ありがとう。
編集:より具体的には、私は1秒刻みのタイマーにしかアクセスできません。
ゲームの最後の2〜4秒のFPSを計算したいと思います。これを行うための最良の方法は何でしょうか?
ありがとう。
編集:より具体的には、私は1秒刻みのタイマーにしかアクセスできません。
ごく最近の投稿のニアミス。指数加重移動平均の使用に関する私の回答を参照してください。
これがサンプルコードです。
最初は:
avgFps = 1.0; // Initial value should be an estimate, but doesn't matter much.
毎秒(最後の1秒間のフレームの総数がであると仮定framesThisSecond
):
// Choose alpha depending on how fast or slow you want old averages to decay.
// 0.9 is usually a good choice.
avgFps = alpha * avgFps + (1.0 - alpha) * framesThisSecond;
これがあなたのために働くかもしれない解決策です。これをpseudo/Cで記述しますが、このアイデアをゲームエンジンに適合させることができます。
const int trackedTime = 3000; // 3 seconds
int frameStartTime; // in milliseconds
int queueAggregate = 0;
queue<int> frameLengths;
void onFrameStart()
{
frameStartTime = getCurrentTime();
}
void onFrameEnd()
{
int frameLength = getCurrentTime() - frameStartTime;
frameLengths.enqueue(frameLength);
queueAggregate += frameLength;
while (queueAggregate > trackedTime)
{
int oldFrame = frameLengths.dequeue();
queueAggregate -= oldFrame;
}
setAverageFps(frameLength.count() / 3); // 3 seconds
}
最後の100フレームのフレーム時間の循環バッファーを保持し、それらを平均化できますか?これが「最後の100フレームのFPS」になります。(または、99、最新の時刻と最も古い時刻を区別しないためです。)
ミリ秒以上の正確なシステム時間を呼び出します。
実際に必要なのは、(mainLoop内の)次のようなものです。
frames++;
if(time<secondsTimer()){
time = secondsTimer();
printf("Average FPS from the last 2 seconds: %d",(frames+lastFrames)/2);
lastFrames = frames;
frames = 0;
}
構造/配列の処理方法を知っている場合は、この例を2秒ではなく4秒に拡張するのは簡単です。しかし、より詳細なヘルプが必要な場合は、正確なアクセスができない理由を実際に説明する必要があります。タイマー(どのアーキテクチャ、言語)-それ以外の場合、すべては推測のようなものです...