Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get timestamp of tick precision in .NET / C#?
    primarykey
    data
    text
    <p>Up until now I used <code>DateTime.Now</code> for getting timestamps, but I noticed that if you print <code>DateTime.Now</code> in a loop you will see that it increments in descrete jumps of approx. 15 ms. But for certain scenarios in my application I need to get the most accurate timestamp possible, preferably with tick (=100 ns) precision. Any ideas? </p> <p><strong>Update:</strong></p> <p>Apparently, <code>StopWatch</code> / <code>QueryPerformanceCounter</code> is the way to go, but it can only be used to measure time, so I was thinking about calling <code>DateTime.Now</code> when the application starts up and then just have <code>StopWatch</code> run and then just add the elapsed time from <code>StopWatch</code> to the initial value returned from <code>DateTime.Now</code>. At least that should give me accurate relative timestamps, right? What do you think about that (hack)? </p> <p><strong>NOTE:</strong></p> <p><code>StopWatch.ElapsedTicks</code> is different from <code>StopWatch.Elapsed.Ticks</code>! I used the former assuming 1 tick = 100 ns, but in this case 1 tick = 1 / <code>StopWatch.Frequency</code>. So to get ticks equivalent to DateTime use <code>StopWatch.Elapsed.Ticks</code>. I just learned this the hard way.</p> <p><strong>NOTE 2:</strong></p> <p>Using the StopWatch approach, I noticed it gets out of sync with the real time. After about 10 hours, it was ahead by 5 seconds. So I guess one would have to resync it every X or so where X could be 1 hour, 30 min, 15 min, etc. I am not sure what the optimal timespan for resyncing would be since every resync will change the offset which can be up to 20 ms. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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