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

c++ boost calculate time spent in function

本文关键字:时间 boost 计算 函数 c++      更新时间:2023-10-16

嗨,又有一个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 elapsed = end - start;
}

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

谢谢

根据boost文档,local_time可能无法在Win32系统上实现微秒级分辨率:

ptime microsec_clock: local_time ()

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

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