C++:计算移动 FPS

发布于 2024-10-12 03:00:18 字数 79 浏览 3 评论 0原文

我想计算游戏最后2-4秒的FPS。最好的方法是什么?

谢谢。

编辑:更具体地说,我只能访问以一秒为增量的计时器。

I would like to calculate the FPS of the last 2-4 seconds of a game. What would be the best way to do this?

Thanks.

Edit: To be more specific, I only have access to a timer with one second increments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

誰ツ都不明白 2024-10-19 03:00:18

差点错过了最近的一个帖子。请参阅我对使用指数加权移动平均线的回复。

C++:计算游戏中的总帧数

这是示例代码。

最初:

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;

Near miss of a very recent posting. See my response there on using exponential weighted moving averages.

C++: Counting total frames in a game

Here's sample code.

Initially:

avgFps = 1.0; // Initial value should be an estimate, but doesn't matter much.

Every second (assuming the total number of frames in the last second is in 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;
难以启齿的温柔 2024-10-19 03:00:18

这是一个可能适合您的解决方案。我将用伪/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
}

Here's a solution that might work for you. I'll write this in pseudo/C, but you can adapt the idea to your game engine.

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
}
半暖夏伤 2024-10-19 03:00:18

可以保留最后 100 帧的帧时间的循环缓冲区,并对它们进行平均吗?这将是“最后 100 帧的 FPS”。 (或者更确切地说,99,因为您无法区分最新时间和最旧时间。)

调用一些准确的系统时间,毫秒或更好。

Could keep a circular buffer of the frame time for the last 100 frames, and average them? That'll be "FPS for the last 100 frames". (Or, rather, 99, since you won't diff the newest time and the oldest.)

Call some accurate system time, milliseconds or better.

木有鱼丸 2024-10-19 03:00:18

你真正想要的是这样的(在你的 mainLoop 中):

frames++;
if(time<secondsTimer()){
  time = secondsTimer();
  printf("Average FPS from the last 2 seconds: %d",(frames+lastFrames)/2);
  lastFrames = frames;
  frames = 0;
}

如果你知道如何处理结构/数组,那么你应该很容易将此示例扩展到 4 秒而不是 2 秒。但是如果你想要更详细的帮助,你真的应该提到为什么你无法访问精确的计时器(哪种架构,语言) - 否则一切就像猜测......

What you actually want is something like this (in your mainLoop):

frames++;
if(time<secondsTimer()){
  time = secondsTimer();
  printf("Average FPS from the last 2 seconds: %d",(frames+lastFrames)/2);
  lastFrames = frames;
  frames = 0;
}

If you know, how to deal with structures/arrays it should be easy for you to extend this example to i.e. 4 seconds instead of 2. But if you want more detailed help, you should really mention WHY you haven't access to an precise timer (which architecture, language) - otherwise everything is like guessing...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文