如何使用 C++ 限制循环中的 FPS?
How to limit FPS in a loop with C++?
我正在尝试在执行交集检查的循环中限制每秒的帧数,使用带有计时和线程的 C++。
这是我的代码:
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::time_point lastFrame = std::chrono::system_clock::now();
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
now = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> delta = now - lastFrame;
lastFrame = now;
if (delta.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - delta.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
printf("Time: %f \n", delta.count());
// Perform intersection test
}
我遇到的问题是 delta 的所有其他输出都显示极小的数量,而不是我想要的 ~200 毫秒/帧:
Time: 199.253200
Time: 2.067700
Time: 199.420400
Time: 2.408100
Time: 199.494200
Time: 2.306200
Time: 199.586800
Time: 2.253400
Time: 199.864000
Time: 2.156500
Time: 199.293800
Time: 2.075500
Time: 201.787500
Time: 4.426600
Time: 197.304100
Time: 4.530500
Time: 198.457200
Time: 3.482000
Time: 198.365300
Time: 3.415400
Time: 198.467400
Time: 3.595000
Time: 199.730100
Time: 3.373400
关于为什么会发生这种情况有什么想法吗?
交替增量时间是由一个逻辑问题引起的:您要根据前一帧的持续时间(根据帧持续时间的计算方式)向一帧添加延迟。这意味着在一个长帧(~200 毫秒)之后你不应用延迟并得到一个短帧(几毫秒),然后触发下一帧的延迟给出一个长帧,依此类推。
如果您考虑一下您的代码是如何工作的,您会发现它完全按照您编写的方式工作。由于代码中的逻辑错误导致 Delta 振荡。
事情是这样的:
- 我们从
delta == 0
开始。
- 因为增量小于
200
,你的代码休眠了 200 - delta(0) == 200
毫秒。
- 现在,delta 本身变得接近
200
(因为您已经测量了睡眠时间和实际工作)并且您睡了 200 - delta(200) == 0
毫秒。
- 之后循环重复。
要解决此问题,您不需要测量睡眠时间。
这是可以做到的:
#include <iostream>
#include <cstdio>
#include <chrono>
#include <thread>
std::chrono::system_clock::time_point a = std::chrono::system_clock::now();
std::chrono::system_clock::time_point b = std::chrono::system_clock::now();
int main()
{
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
a = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> work_time = a - b;
if (work_time.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - work_time.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
b = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> sleep_time = b - a;
// Your code here
printf("Time: %f \n", (work_time + sleep_time).count());
}
}
此代码为我提供了稳定的增量序列:
Time: 199.057206
Time: 199.053581
Time: 199.064718
Time: 199.053515
Time: 199.053307
Time: 199.053415
Time: 199.053164
Time: 199.053511
Time: 199.053280
Time: 199.053283
我经常这样做:
#include <chrono>
#include <iostream>
int main()
{
using clock = std::chrono::steady_clock;
auto next_frame = clock::now();
while(true)
{
next_frame += std::chrono::milliseconds(1000 / 5); // 5Hz
// do stuff
std::cout << std::time(0) << '\n'; // 5 for each second
// wait for end of frame
std::this_thread::sleep_until(next_frame);
}
}
输出:(每个秒值五个)
1470173964
1470173964
1470173964
1470173964
1470173964
1470173965
1470173965
1470173965
1470173965
1470173965
1470173966
1470173966
1470173966
1470173966
1470173966
这很像 ,但它保留了 OP 问题的语法并且不会下降到 C API。此外,它为帧持续时间创建了一个自定义单位,我认为这对可读性很重要:
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>
int
main()
{
using namespace std;
using namespace std::chrono;
using frames = duration<int64_t, ratio<1, 5>>; // 5Hz
auto nextFrame = system_clock::now();
auto lastFrame = nextFrame - frames{1};;
while (true)
{
// Perform intersection test
this_thread::sleep_until(nextFrame);
cout << "Time: " // just for monitoring purposes
<< duration_cast<milliseconds>(system_clock::now() - lastFrame).count()
<< "ms\n";
lastFrame = nextFrame;
nextFrame += frames{1};
}
}
这为我输出:
Time: 200ms
Time: 205ms
Time: 205ms
Time: 203ms
Time: 205ms
Time: 205ms
Time: 200ms
Time: 200ms
Time: 200ms
...
主要注意事项:
- 记录 5Hz 的简明方法:
using frames = duration<int64_t, ratio<1, 5>>;
- 使用
sleep_until
而不是 sleep_for
,它处理了完成实际工作需要多长时间的未知数。
- 除 I/O 和 here's a library to get rid of that 外不使用
.count()
。
- 无需手动转换单位(例如
/ 1000
)。
- 没有浮点数单位,并不是说它有什么问题。
- 最少需要指定或依赖于明确的单位。
添加 duration I/O library 后,上面的代码会发生以下变化:
#include "chrono_io.h"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
using frames = duration<int64_t, ratio<1, 5>>; // 5Hz
auto nextFrame = system_clock::now();
auto lastFrame = nextFrame - frames{1};;
while (true)
{
// Perform intersection test
this_thread::sleep_until(nextFrame);
// just for monitoring purposes
cout << "Time: " << system_clock::now() - lastFrame << '\n';
lastFrame = nextFrame;
nextFrame += frames{1};
}
}
输出会因平台而异(取决于 system_clock
的 "native duration")。在我的平台上它看起来像这样:
Time: 200042µs
Time: 205105µs
Time: 205107µs
Time: 200044µs
Time: 205105µs
Time: 200120µs
Time: 204307µs
Time: 205136µs
Time: 201978µs
...
我正在尝试在执行交集检查的循环中限制每秒的帧数,使用带有计时和线程的 C++。
这是我的代码:
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::time_point lastFrame = std::chrono::system_clock::now();
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
now = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> delta = now - lastFrame;
lastFrame = now;
if (delta.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - delta.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
printf("Time: %f \n", delta.count());
// Perform intersection test
}
我遇到的问题是 delta 的所有其他输出都显示极小的数量,而不是我想要的 ~200 毫秒/帧:
Time: 199.253200
Time: 2.067700
Time: 199.420400
Time: 2.408100
Time: 199.494200
Time: 2.306200
Time: 199.586800
Time: 2.253400
Time: 199.864000
Time: 2.156500
Time: 199.293800
Time: 2.075500
Time: 201.787500
Time: 4.426600
Time: 197.304100
Time: 4.530500
Time: 198.457200
Time: 3.482000
Time: 198.365300
Time: 3.415400
Time: 198.467400
Time: 3.595000
Time: 199.730100
Time: 3.373400
关于为什么会发生这种情况有什么想法吗?
交替增量时间是由一个逻辑问题引起的:您要根据前一帧的持续时间(根据帧持续时间的计算方式)向一帧添加延迟。这意味着在一个长帧(~200 毫秒)之后你不应用延迟并得到一个短帧(几毫秒),然后触发下一帧的延迟给出一个长帧,依此类推。
如果您考虑一下您的代码是如何工作的,您会发现它完全按照您编写的方式工作。由于代码中的逻辑错误导致 Delta 振荡。
事情是这样的:
- 我们从
delta == 0
开始。 - 因为增量小于
200
,你的代码休眠了200 - delta(0) == 200
毫秒。 - 现在,delta 本身变得接近
200
(因为您已经测量了睡眠时间和实际工作)并且您睡了200 - delta(200) == 0
毫秒。 - 之后循环重复。
要解决此问题,您不需要测量睡眠时间。
这是可以做到的:
#include <iostream>
#include <cstdio>
#include <chrono>
#include <thread>
std::chrono::system_clock::time_point a = std::chrono::system_clock::now();
std::chrono::system_clock::time_point b = std::chrono::system_clock::now();
int main()
{
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
a = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> work_time = a - b;
if (work_time.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - work_time.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
b = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> sleep_time = b - a;
// Your code here
printf("Time: %f \n", (work_time + sleep_time).count());
}
}
此代码为我提供了稳定的增量序列:
Time: 199.057206
Time: 199.053581
Time: 199.064718
Time: 199.053515
Time: 199.053307
Time: 199.053415
Time: 199.053164
Time: 199.053511
Time: 199.053280
Time: 199.053283
我经常这样做:
#include <chrono>
#include <iostream>
int main()
{
using clock = std::chrono::steady_clock;
auto next_frame = clock::now();
while(true)
{
next_frame += std::chrono::milliseconds(1000 / 5); // 5Hz
// do stuff
std::cout << std::time(0) << '\n'; // 5 for each second
// wait for end of frame
std::this_thread::sleep_until(next_frame);
}
}
输出:(每个秒值五个)
1470173964
1470173964
1470173964
1470173964
1470173964
1470173965
1470173965
1470173965
1470173965
1470173965
1470173966
1470173966
1470173966
1470173966
1470173966
这很像
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>
int
main()
{
using namespace std;
using namespace std::chrono;
using frames = duration<int64_t, ratio<1, 5>>; // 5Hz
auto nextFrame = system_clock::now();
auto lastFrame = nextFrame - frames{1};;
while (true)
{
// Perform intersection test
this_thread::sleep_until(nextFrame);
cout << "Time: " // just for monitoring purposes
<< duration_cast<milliseconds>(system_clock::now() - lastFrame).count()
<< "ms\n";
lastFrame = nextFrame;
nextFrame += frames{1};
}
}
这为我输出:
Time: 200ms
Time: 205ms
Time: 205ms
Time: 203ms
Time: 205ms
Time: 205ms
Time: 200ms
Time: 200ms
Time: 200ms
...
主要注意事项:
- 记录 5Hz 的简明方法:
using frames = duration<int64_t, ratio<1, 5>>;
- 使用
sleep_until
而不是sleep_for
,它处理了完成实际工作需要多长时间的未知数。 - 除 I/O 和 here's a library to get rid of that 外不使用
.count()
。 - 无需手动转换单位(例如
/ 1000
)。 - 没有浮点数单位,并不是说它有什么问题。
- 最少需要指定或依赖于明确的单位。
添加 duration I/O library 后,上面的代码会发生以下变化:
#include "chrono_io.h"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
using frames = duration<int64_t, ratio<1, 5>>; // 5Hz
auto nextFrame = system_clock::now();
auto lastFrame = nextFrame - frames{1};;
while (true)
{
// Perform intersection test
this_thread::sleep_until(nextFrame);
// just for monitoring purposes
cout << "Time: " << system_clock::now() - lastFrame << '\n';
lastFrame = nextFrame;
nextFrame += frames{1};
}
}
输出会因平台而异(取决于 system_clock
的 "native duration")。在我的平台上它看起来像这样:
Time: 200042µs
Time: 205105µs
Time: 205107µs
Time: 200044µs
Time: 205105µs
Time: 200120µs
Time: 204307µs
Time: 205136µs
Time: 201978µs
...