[英]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 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
有關為什么會發生這種情況的任何想法?
如果您考慮代碼的工作原理,您會發現它的編寫方式與您的代碼完全一致。 Delta由於代碼中的邏輯錯誤而振盪。
這是發生的事情:
delta == 0
開始。 200
,因此您可以將睡眠設置為200 - delta(0) == 200
ms。 200
(因為你已經測量了睡眠時間以及實際工作)並且你睡眠200 - delta(200) == 0
ms。 要解決此問題,您無需測量睡眠時間。
這是如何做到的:
#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
這很像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};
}
}
這為我輸出:
Time: 200ms
Time: 205ms
Time: 205ms
Time: 203ms
Time: 205ms
Time: 205ms
Time: 200ms
Time: 200ms
Time: 200ms
...
要注意的關鍵事項:
using frames = duration<int64_t, ratio<1, 5>>;
sleep_until
而不是sleep_for
,它可以處理未完成實際工作所需的時間。 .count()
, 這里有一個庫可以擺脫它 。 / 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};
}
}
輸出將根據平台而不同(取決於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
...
我通常做這樣的事情:
#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
交替增量時間是由邏輯問題引起的:您正在根據幀之前的持續時間(根據計算幀持續時間的方式)向一幀添加延遲。 這意味着在長幀(~200ms)之后,您不應用延遲並獲得短幀(幾毫秒),然后在下一幀上觸發延遲,從而產生長幀,依此類推。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.