开发者

c++ boost calculate time spent in function

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜