使用High_resolution_clock进行的时间测量无法正常工作 [英] Time measurements with High_resolution_clock not working as intended

查看:225
本文介绍了使用High_resolution_clock进行的时间测量无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用Clock类来测量经过的时间(对于帧时间)。 (问题在代码下面描述。)

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

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

class Clock
{
    timePt currentTime;
    timePt lastTime;

public:
    Clock();

    void update();
    uint64_t deltaTime();

};



Clock.cpp



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();
}

当我尝试像这样使用Clock时

When I try to use Clock like so

Clock clock;

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

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

对我来说,它打印约30次 0直到它最终打印出一个始终非常接近 15625微秒(15.625毫秒)之类的数字。

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).

我的问题是,为什么不存在之间有什么?我想知道我的实现是错误的还是high_resolution_clock的精度表现不佳。有什么想法吗?

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?

编辑:我在Windows 8计算机上将代码块与mingw32编译器一起使用。

I am using Codeblocks with mingw32 compiler on a windows 8 computer.

EDIT2:
我尝试运行以下应显示high_resolution_clock精度的代码:

I tried running the following code that should display high_resolution_clock precision:

template <class Clock>
void display_precision()
{
    typedef std::chrono::duration<double, std::nano> NS;
    NS ns = typename Clock::duration(1);
    std::cout << ns.count() << " ns\n";
}

int main()
{
    display_precision<std::chrono::high_resolution_clock>();
}

对我来说,它显示: 1000 ns。所以我猜high_resolution_clock的精度是1微秒吧?然而,在我的测试中,它似乎具有16毫秒的精度?

For me it prints: "1000 ns". So I guess high_resolution_clock has a precision of 1 microsecond right? Yet in my tests it seems to have a precision of 16 milliseconds?

推荐答案

您正在使用什么系统? (我猜是Windows?已知Visual Studio出现了此问题,现已在VS 2015中修复,请参见错误报告)。在某些系统上, high_resolution_clock 被定义为 system_clock 的别名,它可能具有非常低的分辨率,例如16 ms正在看到。
例如,请参见此问题

What system are you using? (I guess it's Windows? Visual Studio is known to had this problem, now fixed in VS 2015, see the bug report). On some systems high_resolution_clock is defined as just an alias to system_clock, which can have really low resolution, like 16 ms you are seeing. See for example this question.

这篇关于使用High_resolution_clock进行的时间测量无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆