[date-time] Getting time from midnight in nanoseconds
I am resending this with proper subject line. Thanks From: Priyank Patel Sent: Friday, October 22, 2010 8:47 AM To: boost-users@lists.boost.org Subject: Getting time from midnight in nanoseconds Hi, Can you please tell me if there is a way to get current time in nanoseconds using boost date_time library? I have tried following but it doesn't seems to be returning values in nanoseconds. I know I have used microsec_clock and that might be the problem but not sure what alternate to use. Any help will be appreciated. hrtime_t getCurrentTime() { boost::posix_time::ptime recvTime = boost::posix_time::microsec_clock::local_time(); boost::posix_time::time_duration duration( recvTime.time_of_day() ); return duration.total_nanoseconds(); // This call returns values in microseconds (last three digits are always zero in returned value) } Thanks Priyank
On 22.10.2010 15:48, ppatel@efs-us.com wrote:
Can you please tell me if there is a way to get current time in nanoseconds using boost date_time library?
I have tried following but it doesn’t seems to be returning values in nanoseconds. I know I have used microsec_clock and that might be the problem but not sure what alternate to use. [...] return duration.total_nanoseconds(); // This call returns values in microseconds (last three digits are always zero in returned value)
Hi, as far as I remember microsecond_clock uses the highest resolution clock available on your system. On Windows this is a clock with microsecond resolution (in fact it seems to be even more than one microsecond). If you are requesting nanoseconds and nanoseconds are not supported by your system clock all unknowns will be filled with 0. I do not know of any other solution than yours, so I'm afraid you'll have to stick to your current resolution. hth, Norbert
Thanks for the reply. I am running on "linux/Ubuntu (Lucid)" version. Not sure if there is any API in boost like "clock_gettime()" that will return number of nanoseconds monotonically starting at unspecified point/midnight (either way). My goal is to measure how much time a particular API calls takes to run by getting end and start time in nanoseconds. Thanks again, Priyank -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Norbert Wenzel Sent: Friday, October 22, 2010 10:05 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [date-time] Getting time from midnight in nanoseconds On 22.10.2010 15:48, ppatel@efs-us.com wrote:
Can you please tell me if there is a way to get current time in nanoseconds using boost date_time library?
I have tried following but it doesn't seems to be returning values in nanoseconds. I know I have used microsec_clock and that might be the problem but not sure what alternate to use. [...] return duration.total_nanoseconds(); // This call returns values in microseconds (last three digits are always zero in returned value)
Hi, as far as I remember microsecond_clock uses the highest resolution clock available on your system. On Windows this is a clock with microsecond resolution (in fact it seems to be even more than one microsecond). If you are requesting nanoseconds and nanoseconds are not supported by your system clock all unknowns will be filled with 0. I do not know of any other solution than yours, so I'm afraid you'll have to stick to your current resolution. hth, Norbert _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 10/22/2010 10:25 AM, ppatel@efs-us.com wrote:
Thanks for the reply. I am running on "linux/Ubuntu (Lucid)" version. Not sure if there is any API in boost like "clock_gettime()" that will return number of nanoseconds monotonically starting at unspecified point/midnight (either way).
Why not use clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) then? Last I checked, Boost didn't supply a high-res stopwatch. There was one in the Intel threading library, but it was using CLOCK_REALTIME on Linux which was not monotonic! You might also look at rdtsc if it's on your processor.
My goal is to measure how much time a particular API calls takes to run by getting end and start time in nanoseconds.
You need a start "timestamp", an end "timestamp", and a way to find the difference in nanoseconds. So why bring midnight into it? That introduces all the complexity of time zones, clock synchronization, daylight saving time, leap seconds, etc. - Marsh
participants (3)
-
Marsh Ray
-
Norbert Wenzel
-
ppatel@efs-us.com