c++ boost calculate time spent in function

天大地大妈咪最大 提交于 2019-12-24 07:40:44

问题


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


回答1:


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)



来源:https://stackoverflow.com/questions/6735123/c-boost-calculate-time-spent-in-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!