我正在尝试使用带有计时和线程的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
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是delta的每个其他输出显示的是微不足道的数量,而不是我想要的~200 ms /帧:
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
Run Code Online (Sandbox Code Playgroud)
有关为什么会发生这种情况的任何想法?
Hol*_*Cat 18
如果您考虑代码的工作原理,您会发现它的编写方式与您的代码完全一致.Delta由于代码中的逻辑错误而振荡.
这是发生的事情:
delta == 0.200,所以你编码sleeps 200 - delta(0) == 200ms.200(因为你已经测量了睡眠时间和实际工作)并且你睡了200 - delta(200) == 0ms.要解决此问题,您无需测量睡眠时间.
这是如何做到的:
#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());
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码给了我一系列稳定的增量:
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
Run Code Online (Sandbox Code Playgroud)
How*_*ant 12
这很像Galik的答案,但它保留了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};
}
}
Run Code Online (Sandbox Code Playgroud)
这为我输出:
Time: 200ms
Time: 205ms
Time: 205ms
Time: 203ms
Time: 205ms
Time: 205ms
Time: 200ms
Time: 200ms
Time: 200ms
...
Run Code Online (Sandbox Code Playgroud)
要注意的关键事项:
using frames = duration<int64_t, ratio<1, 5>>;sleep_until而不是sleep_for,它可以处理完成实际工作所需的时间..count()除了I/O之外没有使用,这里有一个库可以摆脱它./ 1000).通过添加持续时间I/O库,以下是上述代码的更改方式:
#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};
}
}
Run Code Online (Sandbox Code Playgroud)
输出将根据平台而不同(取决于"原生持续时间" system_clock).在我的平台上,它看起来像这样:
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
...
Run Code Online (Sandbox Code Playgroud)
我通常做这样的事情:
#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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:(每秒五个值)
1470173964
1470173964
1470173964
1470173964
1470173964
1470173965
1470173965
1470173965
1470173965
1470173965
1470173966
1470173966
1470173966
1470173966
1470173966
Run Code Online (Sandbox Code Playgroud)