0

How to get High Accurate/close to accuracy timestamp in MICROSECONDS(system time) in a multithreaded/threadsafe C programming in windows 7 (In 2015).

Probably timestamp could be Year:Day:Month:Hour:Mins:Secs:Millisecs:Microsecs

Lo1234
  • 45
  • 1
  • 8
  • I was looking for some code/approach in a threadsafe way – Lo1234 Apr 10 '15 at 04:05
  • That approach does seem to describe threadsafe ways to get high resolution time values. – Brian Cain Apr 10 '15 at 04:15
  • 1
    `QueryPerformanceCounter` is safe, but not directly related to wall time. You could store the system time when your program started and do some math with the offset. There aren't really any other solutions that are guaranteed to have sub-millisecond resolution until Windows 8 with `GetSystemTimePreciseAsFileTime`. – Retired Ninja Apr 10 '15 at 04:57
  • [MSDN: "Acquiring high-resolution time stamps"](https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408%28v=vs.85%29.aspx) – Arno Apr 10 '15 at 07:12
  • I tried to use boost/chrono.... using boost::chrono::high_resolution_clock::now().time_since_epoch() I am not able to get the system time instead I get Thu Jan 01 09:53:53 1970... what could be the problem? but with std::chrono I am able to get the correct system time.. – Lo1234 Apr 10 '15 at 21:51

1 Answers1

1

If you're looking for real-world timestamps and not just time-interval-measurement (as suggested by your comment that timestamps could be in Year:Day:Month:Hour:Mins:Secs:Millisecs:Microsecs format), then GetSystemTimeAsFileTime() is probably the best thing you're going to get. It populates a FILETIME struct, which uses units of 100nS, although the actual resolution of the data provided is almost certainly not going to be as high-resolution as the FILETIME struct's units would imply (apparently the designer of the FILETIME struct wanted to leave plenty of room for future improvement!)

Once you have your FILETIME object, you can then convert it to a SYSTEMTIME object using FileTimeToSystemTime(). The SYSTEMTIME object breaks the time out into year/month/day/hours/minutes/seconds/milliseconds fields (sorry, no microseconds).

On the other hand, if your purpose is actually only to measure time intervals , and you don't need to match them to absolute real-world dates/times, then you have some other options. The most precise API would be QueryPerformanceCounter(), but even then I wouldn't get my hopes up too high -- Windows is not a real-time OS, and its lack of real-time performance will be reflected in the quality of the results you get from it.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • QueryPerformanceCounter will be a wise choice.. – User1611 Apr 10 '15 at 09:50
  • Cant we use "chrono" for microsecond precision? – Lo1234 Apr 10 '15 at 14:19
  • The [GetSystemTimePreciseAsFileTime](https://msdn.microsoft.com/en-us/library/windows/desktop/hh706895%28v=vs.85%29.aspx) function may overcome the granularity/resolution limitation. However, this function only became available with Windows 8 and it shows misleading results when a system time adjustment is active. – Arno Apr 10 '15 at 14:34
  • @Lo1234 you can use chrono, but the authors of the Windows implementation of chrono still have to call some underlying Windows API, so it's unlikely you'll get better performance by using chrono than you would by calling that same Windows API directly (you will get better portability though) – Jeremy Friesner Apr 10 '15 at 14:43
  • can you show me some code please? – Lo1234 Apr 10 '15 at 20:41
  • 1
    No, you can Google for it as well as I can. – Jeremy Friesner Apr 11 '15 at 03:02