boost :: chrono nanoseconde windows7 [英] boost::chrono nanoseconde windows7

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

问题描述

  #include< boost / chrono.hpp> 

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

//烧一些时间

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

auto val = ns.count();
}

在Windows 7 64位和boost 1.48可以肯定上面的代码

编辑:在做一些测试后:

  #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!\\\
;

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。 <<'\\\
';

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

std :: cin.ignore();
}

两个方法返回近似相同的结果。上面的代码奇怪是在第一个方法中调用的方法总是返回最好的结果,在第二个位置切换boost,GetCounter将是最好的(即:更接近睡眠持续时间)

解决方案

当我浏览文档时,看起来两次之间的差异返回 duration ,而 high_resolution_clock 可能是 system_clock steady_clock (取决于操作系统的API是否有稳定的时钟)。 steady_clock :: duration nanoseconds system_clock docs说嵌套持续时间typedef具有取决于平台提供的分辨率。



但是, duration 类型可以在单位之间转换。因此, sec 将始终以纳秒为单位,但不能保证其可以精确到我可以找到的纳秒。



在未记录的详细信息中, http://www.boost.org/doc/libs/1_48_0/boost/chrono/system_clocks.hpp 显示,当定义 BOOST_CHRONO_WINDOWS_API 时,分辨率为100ns,对于所有其他API分辨率为1ns。结果是在所有情况下存储为纳秒,因此您不必担心转换。


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

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 ?

EDIT: After doing some test :

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

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)

解决方案

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.

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 :: chrono nanoseconde windows7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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