C ++ Boost计算功能花费的时间 [英] c++ boost calculate time spent in function

查看:239
本文介绍了C ++ Boost计算功能花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次遇到一个boost问题:我需要计算在boost线程中函数所花费的时间:这是代码:

  boost :: posix_time :: microseconds tes(12); 
int i = 0;
while(true)
{
boost :: posix_time :: ptime start = boost :: posix_time :: microsec_clock :: local_time();

myFunction();

boost :: this_thread :: sleep(tes);

boost :: posix_time :: ptime end = boost :: posix_time :: microsec_clock :: local_time();

boost :: posix_time :: time_duration经过=结束-开始;
}

所以我尝试了很多次,但是经过的time_duration始终为0,我已经添加以测试12微秒的睡眠功能,因此以最佳方式我将经过12微秒,但仍为0。
我需要告诉线程在读取时间后更新计时器? p>

谢谢

解决方案

根据加强文档 local_time 可能无法在Win32系统上达到微秒级别的分辨率:


ptime microsec_clock :: local_time()


获取本地时间使用亚秒分辨率时钟。在Unix系统上,这是使用GetTimeOfDay实现的。在大多数Win32平台上,它是使用ftime实现的。 Win32系统通常无法通过此API达到微秒级的分辨率。如果更高的分辨率对您的应用程序至关重要,请测试您的平台以查看达到的分辨率。


可能唯一的选择是使用高分辨率计时器(非便携式)


Hi again with a boost question: I need to calculate the time spent in my function in my boost thread: here is the code:

boost::posix_time::microseconds tes( 12 );
int i = 0;
while( true )
{
    boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time( );

    myFunction( );

    boost::this_thread::sleep( tes );   

    boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time( );

    boost::posix_time::time_duration elapsed = end - start;
}

so I tried many times but the elapsed time_duration is always 0, I've added to test the sleep function of 12 microsecs, so in the best ways I will have 12 microsec elapsed but is still 0.. I need to tell to the thread to update the timer after the time read??

thanks

解决方案

According to boost documentation, local_time might not achieve microsecond level resolution on Win32 system:

ptime microsec_clock::local_time()

Get the local time using a sub second resolution clock. On Unix systems this is implemented using GetTimeOfDay. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution.

Possibly your only option is using a high resolution timer (not portable)

这篇关于C ++ Boost计算功能花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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