Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes Stopwatch.Gettimestamp ever roll over? Or roll back?
    primarykey
    data
    text
    <p>In using Stopwatch.GetTimestamp() we find that if you record the return value and then continue calling it and comparing to the previous return value, it will eventually but unpredictably return a value less than the original.</p> <p>Is this expected behavior?</p> <p>The purpose of doing this in the production code is to have a microsecond accurate sytem time.</p> <p>The technique involves calling DateTime.UtcNow and also calling Stopwatch.GetTimestamp() as originalUtcNow and originalTimestamp, respectively.</p> <p>From that point forward, the application simply calls Stopwatch.GetTimestamp() and using Stopwatch.Frequency it calculates the difference from the originalTimestamp variable and then adds that difference to the originalUtcNow.</p> <p>Then, Voila...an efficient and accurate microsecond DateTime.</p> <p>But, we find that sometimes the Stopwatch.GetTimestamp() will return lower number.</p> <p>It happens quite rarely. Our thinking is to simply "reset" when that happens and continue.</p> <p>HOWEVER, it makes us doubt the accuracy of the Stopwatch.GetTimestamp() or suspect there is a bug in the .Net library.</p> <p>If you can shed some light on this, please do.</p> <p>FYI, based on the current timestamp value, the frequence, and the long.MaxValue it appears unlikely that it will roll over during our lifetime unless it's a hardware issue.</p> <p>EDIT: We're now calculating this value "per thread" and then "clamping it" to watch for jumps between cores to reset it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload