gpt4 book ai didi

c++ - 为什么我的速率限制器强制执行与指定速率不同的速率?

转载 作者:行者123 更新时间:2023-12-02 10:19:17 25 4
gpt4 key购买 nike

问题:我正在从头开始编写游戏(作为编程练习)。我试图限制每秒的游戏逻辑循环(“滴答声”)数量。我将其设置为任意100滴答/秒。但是无论我做什么,它似乎都以〜130 ticks / second的速度运行。可能是舍入错误加起来吗?还有吗在此先感谢您提供的任何帮助。

注意:我的代码库比这个大得多,但是出于这个问题的目的,我在不破坏速率限制器的情况下尽可能地简化了代码。

输出:

counter 1 sleep_for(5ms)
counter 2 sleep_for(2ms)
[snip]
counter 132 sleep_for(3ms)
counter 133 sleep_for(3ms)
133 TPS last 1003ms
counter 134 sleep_for(3ms)
counter 135 sleep_for(3ms)
[snip]
counter 265 sleep_for(3ms)
counter 266 sleep_for(3ms)
133 TPS last 1004ms
counter 267 sleep_for(3ms)
counter 268 sleep_for(3ms)
[snip]
counter 399 sleep_for(3ms)
counter 400 sleep_for(3ms)
134 TPS last 1006ms

代码:

(要查看的两个主要功能是ThreadRateLimiter::Tock()和TickRateCounter::Tock())
#include <chrono>
#include <exception>
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>

using namespace std;

const int TICK_RATE = 100;
const chrono::milliseconds TIME_PER_TICK =
chrono::duration_cast<chrono::milliseconds>(chrono::seconds(1)) / TICK_RATE;

template <class T>
class TickTocker
{
public:
virtual void Tick() = 0;
virtual T Tock() = 0;
virtual T TickTock()
{
Tick();
return Tock();
}
};

股票代号:
class Ticker : public TickTocker<long>
{
public:
friend ostream& operator<<(ostream& stream, const Ticker& counter);
Ticker() :
Ticker(0)
{}
Ticker(long counter) :
mTicks(counter),
mTicksLast(mTicks)
{}
Ticker(const Ticker& counter) :
Ticker(counter.mTicks)
{}
bool operator==(const long i)
{
return mTicks == i;
}
void Tick() override
{
mTicks++;
}
long Tock() override
{
long diff = mTicks - mTicksLast;
mTicksLast = mTicks;
return diff;
}
// private:
long mTicks;
long mTicksLast;
};

ostream& operator<<(ostream& stream, const Ticker& counter)
{
return (stream << "counter " << counter.mTicks);
}

TickTracker:
class TickTracker : public TickTocker<chrono::milliseconds>
{
public:
TickTracker() :
mTime(chrono::steady_clock::now()),
mLastTime(mTime)
{}
void Tick() override
{
mTime = chrono::steady_clock::now();
}
chrono::milliseconds Tock() override
{
chrono::milliseconds diff = chrono::duration_cast<chrono::milliseconds>(mTime - mLastTime);
mLastTime = mTime;
return diff;
}
protected:
chrono::time_point<chrono::steady_clock> mTime;
chrono::time_point<chrono::steady_clock> mLastTime;
};

ThreadRateLimiter:
class ThreadRateLimiter : public TickTracker
{
public:
ThreadRateLimiter() : TickTracker(),
mMsFast(chrono::milliseconds(0))
{}
void Tick() override
{
mCounter.Tick();
TickTracker::Tick();
if (mCounter == 1)
{
TickTracker::Tock();
}
}
chrono::milliseconds Tock()
{
chrono::milliseconds diff = TickTracker::Tock();
chrono::milliseconds remaining = TIME_PER_TICK - diff;
/*
* If we always sleep the full remaining time, we'll alternate between sleeping for "minimum" and "maximum" sleep
* times. Sleeping the full remaining time only when we exceed the average makes for more stable sleep times.
*/
bool fullSleep = (mMsFast.count() > (TIME_PER_TICK.count() / 2));
mMsFast += remaining;
if (mMsFast.count() > 0)
{
chrono::milliseconds sleep = fullSleep ? mMsFast : (chrono::milliseconds(mMsFast.count() / 2));
cout << mCounter << " sleep_for(" << sleep.count() << "ms)" << endl;
this_thread::sleep_for(mMsFast);
mMsFast -= sleep;
}
mCounter.Tock();
return remaining;
}
private:
Ticker mCounter;
chrono::milliseconds mMsFast;
};

TickRateCounter:
class TickRateCounter : public TickTracker
{
public:
TickRateCounter(string rateLabel) : TickTracker(),
mRateLabel(rateLabel)
{}
void Tick() override
{
mCounter.Tick();
TickTracker::Tick();
}
chrono::milliseconds Tock() override
{
if (chrono::duration_cast<chrono::seconds>(mTime - mLastTime).count() >= 1)
{
chrono::milliseconds duration = TickTracker::Tock();
cout << (mCounter.Tock() / chrono::duration_cast<chrono::seconds>(duration).count()) << " " << mRateLabel
<< " last " << duration.count() << "ms" << endl;
return duration;
}
return chrono::milliseconds(0);
}
// private:
Ticker mCounter;
string mRateLabel;
};

主要:
int main()
{
ThreadRateLimiter mRateLimiter;
TickRateCounter mTpsCounter("TPS"); // TPS = Ticks per second. Tick = one game loop

while (mTpsCounter.mCounter.mTicks < 400)
{
mRateLimiter.TickTock();
mTpsCounter.TickTock();
}

return 0;
}

最佳答案

进行速率限制的正确方法是:

//pseudocode follows
const frame_duration = something;
last = now();
while(true)
{
process_your_frame_here()

do
{
t = now();
sleep(0); // or whatever fits your system
}
while(t < last + frame_duration);

last = last + frame_duration; // THIS IS KEY
// last = now; // would not produce the right framerate
}

基本上,您会像看起来那样延迟帧。但是当记账所花费的时间时,只需添加所需的时间(frame_duration)。因此,在多个帧上,它变得均匀。

详细地说,如果 now()从1000(无论以什么单位)开始,并且您将 frame_duration设置为200,则帧1仅在1200之后运行,帧2仅在1400之后运行,...帧20100之后才运行,从而在很长一段时间内提供精确的帧速率。

关于c++ - 为什么我的速率限制器强制执行与指定速率不同的速率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60921680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com