How to get a reliable millisecond timestamp? RRS feed

  • Question

  • Hi there

    I am using the DateTime::Now class to get the milliseconds of a specific time. This does not work 100%. I can tell this, because my program is tracking a joystick and measuring position and velocity - and sometimes (every 5 or so samples) two samples will have exactly the same timestamp down to the millisecond. This is rather weird, knowing that my loop only runs every 8th millisecond - and I can see that the joystick-data have changed (so the loop records just fine).

    Is there any more reliable and precise way of measuring time than the DateTime instance?

    Kind regards
    Thursday, December 3, 2009 8:02 PM


All replies

  • Instead of using DateTime, you should use the performance counters.  They are much, much more reliable.

    You'll want to use QueryPerformanceCounter and QueryPerformanceFrequency.  They provide access to the high performance counters in Windows.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by jinzai Thursday, December 3, 2009 9:37 PM
    • Marked as answer by Nancy Shao Thursday, December 10, 2009 9:09 AM
    Thursday, December 3, 2009 8:18 PM
  • Also, understand if you are triggering your loop from a timer of some sort, your code may not execute when you expect -- this is normal due to preemptive multi-tasking.

    However, I have certainly tracked a 20ms loop in my code, and frequently seen intervals like:

    Thursday, December 3, 2009 8:34 PM
  • If you don't really need to have true millisecond precision or accuracy, you could always maintain a last time stamp and a counter.  Bump the counter whenever the last time stamp matches the current one and then add the counter to the current millisecond part of the current time stamp.  This is often good enough for most applications and involves mush less system overhead.  Use the perf counters only if you really need that kind of timing precision and accuracy.  From your description, it seems as though you might not need the millisecond accuracy.

    Note that this solution can lead to non-monotonic time sequences if your "loop" suddenly runs much faster.  You can trap this case by detecting when the counter exceeds the minimum period your loop should be running at.  In your case that would be 8.  Since you have reported seeing as many as two identical timestamps, you probably won't ever encounter this issue.

    Joseph w Donahue joseph@odonahue.com www.odonahue.com
    Thursday, December 3, 2009 9:26 PM
  • Hello Michael,

    If you want a precise way of measuring time, you can use QueryPerformanceCounter and QueryPerformanceFrequency as Reed's suggestion, they are high precision. And please check this thread with following link which includes lots of ways to measure time:


    Best Regards,
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, December 7, 2009 8:34 AM