问题
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