我想计算游戏最后2-4秒的FPS。最好的方法是什么?
谢谢。
编辑:更具体地说,我只能使用每秒增量为1秒的计时器。
我想计算游戏最后2-4秒的FPS。最好的方法是什么?
谢谢。
编辑:更具体地说,我只能使用每秒增量为1秒的计时器。
刚刚发布的帖子差点就错过了。请参考我在那里关于使用指数加权移动平均值的回复。
以下是示例代码:
最初:
avgFps = 1.0; // Initial value should be an estimate, but doesn't matter much.
每秒钟(假设上一秒中的总帧数为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;
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帧,因为不会计算最新时间和最旧时间之间的差异)。
调用一些准确的系统时间,以毫秒为单位或更高精度。
frames++;
if(time<secondsTimer()){
time = secondsTimer();
printf("Average FPS from the last 2 seconds: %d",(frames+lastFrames)/2);
lastFrames = frames;
frames = 0;
}
如果你知道如何处理结构体/数组,那么将这个示例扩展到4秒应该很容易。但是如果你需要更详细的帮助,你真的应该提到为什么你没有访问精确计时器(使用的架构、语言),否则一切都像猜测...