c ++ boost计算在函数中花费的时间

时间:2011-07-18 15:13:08

标签: c++ windows boost functional-testing duration

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

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

所以我尝试了很多次但是经过的time_duration总是0,我已经添加了测试12微秒的睡眠功能,所以在最好的方式我会有12微秒过去但仍然是0 .. 我需要告诉线程在读取时间后更新定时器??

感谢

1 个答案:

答案 0 :(得分:4)

根据boost documentationlocal_time可能无法在Win32系统上实现微秒级别的分辨率:

  

ptime microsec_clock :: local_time()

     

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

可能您唯一的选择是使用high resolution timer(非便携式)

相关问题