的boost ::时辰nanoseconde的windows7 [英] boost::chrono nanoseconde windows7

查看:235
本文介绍了的boost ::时辰nanoseconde的windows7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <boost/chrono.hpp>

int main()
{ 
    boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();

    //burn some time

    boost::chrono::nanoseconds ns = boost::chrono::high_resolution_clock::now() - start;

    auto val = ns.count();
}

在Windows 7 64位,提高1.48我可以肯定的是,上述code会很好回报我一个时间纳秒/纳秒precision?

Under Windows 7 64 bits and boost 1.48 could I be sure that the above code will well return me a time in nanosecond/with nanosecond precision ?

编辑:做一些试验后:

  #include <boost/chrono.hpp>
    #include <windows.h>

    double PCFreq = 0.0;
    __int64 CounterStart = 0;

    void StartCounter()
    {
        LARGE_INTEGER li;
        if(!QueryPerformanceFrequency(&li))
            std::cout << "QueryPerformanceFrequency failed!\n";

        PCFreq = double(li.QuadPart)/1000.0;

        QueryPerformanceCounter(&li);
        CounterStart = li.QuadPart;
    }
    double GetCounter()
    {
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return double(li.QuadPart-CounterStart)/PCFreq;
    }

    int main()
    {
        boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
        Sleep(1000);
        boost::chrono::nanoseconds sec = boost::chrono::high_resolution_clock::now() - start;   
        std::cout << sec.count()/1000000. <<'\n';

        StartCounter();
        Sleep(1000);
        std::cout << GetCounter() <<'\n';

        std::cin.ignore(); 
    }

这两个方法的返回近似相同result.Something奇怪上述code是在主invoqued第一种方法将永远排在第二位置返回的最好成绩,开关升压和GetCounter将是最好的(即:更多接近睡眠时间)

both method return approximatively the same result.Something strange about the above code is that first method invoqued in the main will always return the best result, switch boost in second position and GetCounter will be the best (i.e: more closer to Sleep duration)

推荐答案

当我通过文档钻研,它看起来像两个时间之间的区别返回持续时间,和该 high_resolution_clock 可能是 SYSTEM_CLOCK steady_clock (视如果对操作系统的API有一个稳定的时钟)。 steady_clock ::病程纳秒 SYSTEM_CLOCK 文档说嵌套时间的typedef有依赖于由平台提供的一个解决方案。

As I delve through the docs, it looks like the difference between two times returns a duration, and that high_resolution_clock might be a system_clock or a steady_clock (depending on if the OS's API has a steady clock). steady_clock::duration is nanoseconds. system_clock docs say "The nested duration typedef has a resolution that depends on the one provided by the platform."

但是,持续时间类型能够单位之间的转换。因此,总是会在纳秒,但是没有保证,这将是的准确的到,我可以找到纳秒。

However, the duration type is able to convert between units. So sec will always be in nanoseconds, but there's no guarantee that it will be accurate to the nanosecond that I can find.

在无证的详细信息,请 HTTP://www.boost .ORG / DOC /库/ 1_48_0 /升压/计时器/ system_clocks.hpp 表明,当定义 BOOST_CHRONO_WINDOWS_API ,分辨率为100ns,并为所有其他API分辨率为1ns的。其结果是存储为纳秒所有的情况下,所以你永远不要担心转换。

On the undocumented details, http://www.boost.org/doc/libs/1_48_0/boost/chrono/system_clocks.hpp shows that when BOOST_CHRONO_WINDOWS_API is defined, the resolution is 100ns, and for all other APIs resolution is 1ns. The result is in all cases stored as nanoseconds, so you don't ever have to worry about conversions.

这篇关于的boost ::时辰nanoseconde的windows7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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