有人能幫我找出差異在哪裏嗎?因爲第一代碼:std :: chrono不同的結果 - 固定的時間步進循環
#include <iostream>
#include <chrono>
#include <ratio>
using namespace std::chrono;
const nanoseconds timePerFrame = duration_cast<nanoseconds>(duration<steady_clock::rep, std::ratio<1, 60>>(1));
nanoseconds accumulator(0);
nanoseconds counter(0);
steady_clock::time_point begin;
int i = 0;
int main()
{
while(true)
{
begin = steady_clock::now();
while(accumulator >= timePerFrame)
{
accumulator -= timePerFrame;
++i;
}
accumulator += steady_clock::now() - begin;
counter += steady_clock::now() - begin;
if(counter >= seconds(1))
{
std::cout << i << std::endl;
break;
}
}
}
輸出:30,和一個第二代碼:
#include <iostream>
#include <chrono>
#include <ratio>
using namespace std::chrono;
const nanoseconds timePerFrame = duration_cast<nanoseconds>(duration<steady_clock::rep, std::ratio<1, 60>>(1));
nanoseconds accumulator(0);
nanoseconds counter(0);
steady_clock::time_point begin;
steady_clock::time_point end;
int i = 0;
int main()
{
while(true)
{
begin = steady_clock::now();
while(accumulator >= timePerFrame)
{
accumulator -= timePerFrame;
++i;
}
end = steady_clock::now();
accumulator += end - begin;
counter += end - begin;
if(counter >= seconds(1))
{
std::cout << i << std::endl;
break;
}
}
}
輸出:60;
唯一的區別是在第二個示例中使用「結束」變量。在我看來,它不應該造成這樣的差異。我的意思是,不是steady_clock :: now()與end = steady_clock :: now()完全相同?
我忽略了它。謝謝你的快速回復。現在一切都很清楚。 :) –