dimanche 26 juillet 2015

Time measurements with High_resolution_clock not working as intended

I want to be able to measure time elapsed (for frame time) with my Clock class. (Problem described below the code.)

Clock.h

typedef std::chrono::high_resolution_clock::time_point timePt;

class Clock
{
    timePt currentTime;
    timePt lastTime;

public:
    Clock();

    void update();
    uint64_t deltaTime();

};

Clock.cpp

#include "Clock.h"

using namespace std::chrono;

Clock::Clock()
{
    currentTime = high_resolution_clock::now();
    lastTime = currentTime;
}

void Clock::update()
{
    lastTime = currentTime;
    currentTime = high_resolution_clock::now();
}

uint64_t Clock::deltaTime()
{
    microseconds delta = duration_cast<microseconds>(currentTime - lastTime);
    return delta.count();
}

When I try to use Clock like so

Clock clock;

while(1) {
    clock.update();
    uint64_t dt = clock.deltaTime();

    cout << dt << endl; //time elapsed since last update in microseconds
    for (int i=0; i < 10000; i++)
    {
        //do something to waste time between updates
        int k = i*dt;
    } 
 }

For me it prints about 30 times "0" until it finally prints a number which is always very close to something like "15625" microseconds (15.625 milliseconds).

My question is, why isn't there anything between? I'm wondering whether my implementation is wrong or the precision on high_resolution_clock is acting strange. Any ideas?

Aucun commentaire:

Enregistrer un commentaire