Könnte mir jemand helfen, herauszufinden, wo der Unterschied ist? Da erster Code:Std :: Chrono verschiedene Ergebnisse - feste Zeit Schrittschleife
#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;
}
}
}
Ausgänge: 30, und ein zweiter Code:
#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;
}
}
}
Ausgänge: 60;
Der einzige Unterschied ist die Verwendung der Variable "end" im zweiten Beispiel. Meiner Meinung nach sollte es keinen solchen Unterschied verursachen. Ich meine, ist nicht steady_clock :: now() genau das gleiche wie end = steady_clock :: now()?
Ich habe es übersehen. Danke für eine schnelle Anprobe. Jetzt ist alles klar. :) –