分辨率std :: chrono :: high_resolution_clock不对应测量 [英] resolution of std::chrono::high_resolution_clock doesn't correspond to measurements

查看:345
本文介绍了分辨率std :: chrono :: high_resolution_clock不对应测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我通过这个测试程序提出我的问题:

Let me ask my question by this test program:

#include <iostream>
#include <chrono>

using std::chrono::nanoseconds;
using std::chrono::duration_cast;

int main(int argc, char* argv[])
{
    std::cout << "resolution (nano) = " << (double) std::chrono::high_resolution_clock::period::num
        / std::chrono::high_resolution_clock::period::den * 1000 * 1000 * 1000 << std::endl;

    auto t1 = std::chrono::high_resolution_clock::now();
    std::cout << "how much nanoseconds std::cout takes?" << std::endl;
    auto t2 = std::chrono::high_resolution_clock::now();


    auto diff = t2-t1;
    nanoseconds ns = duration_cast<nanoseconds>(diff);

    std::cout << "std::cout takes " << ns.count() << " nanoseconds" << std::endl;
    return 0;
}

在我的机器上输出:

分辨率(纳米)= 100

resolution (nano) = 100

多少纳秒std :: cout需要多少?

how much nanoseconds std::cout takes?

std :: cout需要1000200纳秒

std::cout takes 1000200 nanoseconds

我收到 1000200 1000300 1000400 1000500 code> 1000600 或 2000600 (= 1或2微秒)。显然, std :: chrono 的解析度为 100纳秒 std :: cout 错误。 (为什么我从来没有收到介于1到2微秒之间的东西,例如 1500000 ?)

I receive either 1000200 or 1000300 or 1000400 or 1000500 or 1000600 or 2000600 as a result (= 1 or 2 microsecond). Obviously either the resolution of std::chrono is not 100 nano-seconds or the way I measure the time of std::cout is wrong. (why I never receive something between 1 and 2 microseconds, for example 1500000?)

分辨率定时器在C ++。操作系统本身提供了一个高分辨率的定时器,因为我可以在同一台机器上使用C# Stopwatch 类来测量微秒精度。所以我只需要正确使用操作系统的高分辨率定时器!

I need a high-resolution timer in C++. The OS itself provides a high-resolution timer because I'm able to measure things with microsecond-precision using C# Stopwatch class on the same machine. So I would just need to correctly use the high-resolution timer that the OS has!

如何修复我的程序产生预期的结果?

How do I fix my program to produce the expected results?

推荐答案

我猜你在使用VS2012;如果没有,忽略这个答案。 VS2012 typedef high_resolution_clock system_clock 。可悲的是,这意味着它有精确的精度(大约1ms)。我写了一个更好的高res时钟,使用 QueryPerformanceCounter 在VS2012 ...

I'm going to guess you are using VS2012; If not, disregard this answer. VS2012 typedef's high_resolution_clock to system_clock. Sadly, this means it has crappy precision (around 1ms). I wrote a better high res clock which uses QueryPerformanceCounter for use in VS2012...

HighResClock。 h:

    struct HighResClock
    {
        typedef long long                               rep;
        typedef std::nano                               period;
        typedef std::chrono::duration<rep, period>      duration;
        typedef std::chrono::time_point<HighResClock>   time_point;
        static const bool is_steady = true;

        static time_point now();
    };

HighResClock.cpp:

namespace
{
    const long long g_Frequency = []() -> long long 
    {
        LARGE_INTEGER frequency;
        QueryPerformanceFrequency(&frequency);
        return frequency.QuadPart;
    }();
}

HighResClock::time_point HighResClock::now()
{
    LARGE_INTEGER count;
    QueryPerformanceCounter(&count);
    return time_point(duration(count.QuadPart * static_cast<rep>(period::den) / g_Frequency));
}

(我省略了一个断言和#ifs看看是否被编译2012从上述代码)

(I left out an assert and #ifs to see if it's being compiled on 2012 from the above code)

您可以在任何地方使用此时钟,与标准时钟一样。

You can use this clock anywhere and in the same way as standard clocks.

这篇关于分辨率std :: chrono :: high_resolution_clock不对应测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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