Why is ElapsedTicks X 10,000 not equal to ElapsedMilliseconds for .Net stopwatch?

I am trying to run a test on some code. I use a stopwatch. When I output the number of milliseconds, it always tells me 0, so I thought that I would try the number of ticks. I can see that the number of ticks is between 20,000 and 30,000. Looking at the MSDN in TimeSpan.TicksPerMillisecond, he says it's 10,000 ticks per millisecond. In this case, why the past milliseconds on my stopwatch are not displayed as 2 or 3?

What am I missing? I even returned the result on the same line. This is what I get.

Time taken: 26856 ticks, 0 ms 

And he is permanent.

EDIT (code added) This is my code that I run in a loop. I understand that every time I create a new stopwatch, which is not very effective, but I do not see how this can distort my results. Thanks guys

 Dim SW = New Stopwatch() SW.Reset() SW.Start() MethodCall() SW.Stop() Console.WriteLine(String.Format("Time to increase counters: {0} ticks, {1} ms", SW.ElapsedTicks, SW.ElapsedMilliseconds)) 
+7
performance
source share
2 answers

Stopwatch ticks are different from DateTime Ticks.

The length of the Stopwatch mark depends on the frequency of the stopwatch (one tick is one second divided by the frequency, as described in the MSDN documentation for the .ElapsedTicks stopwatch .

Arguably, Stopwatch.ElapsedTicks was a poor name choice for this property due to potential confusion with DateTime ticks. I would prefer something like ElapsedRawTicks or some other suitable adjectival qualifier to hint that these are not standard Ticks.

+10
source share

Make sure that you really start Stopwatch and use either Stopwatch.ElapsedMilliseconds or Stopwatch.Elapsed.TotalMilliseconds .

0
source share

All Articles