1

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

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
ghiboz
  • 7,863
  • 21
  • 85
  • 131
  • A few questions: (1) what system are you running this on? The boost docs say that on Windows systems you may not get microsecond resolution. (2) have you tried this with a few magnitudes more time? (3) where do you print out the elapsed time? – Abe Schneider Jul 18 '11 at 15:18
  • I'm using under windows xp 32 and windows 7 64.. so in windows only milliseconds? – ghiboz Jul 18 '11 at 15:20
  • 1
    I don't do Windows programming myself, so I'm unsure, but the docs seem to make it sound like there is no guarantee it will can do microsecond accuracy. A quick search seems to indicate that the Windows timer isn't very reliable at the microsecond level. If you have access to a posix machine, I might try to that. Also, what version of Boost are you using? – Abe Schneider Jul 18 '11 at 15:27
  • Try boost::timer. It's intended for this kind of thing. – Nathan Monteleone Jul 18 '11 at 17:34

1 Answers1

4

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)

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • I've tried to change all to milliseconds, but the refresh problem is still alive, the difference is always 0, also if I put a sleep of 10 milliseconds – ghiboz Jul 18 '11 at 15:34
  • 1
    the doc above use the expression "sub second"... millisecond is also sub second. try putting 2 seconds delay... see also my edit... – sergio Jul 18 '11 at 15:43