Datetime Granularity between 32-bit and 64-bit Windows

I created a small engineering application for some network debugging. It accepts a list of IP addresses and pings of them, with a user timeout and speed. It records the average round-trip time, and each time one of the senders fails, it records the duration of the failure and the time stamp when this happened ...

This is an idea. I developed it on a Win7 machine with .Net4 and had to put this thing on a set of XP laptops.

The problem is that my box duration values ​​during testing show good ms durations, when I look, they show 0 or 15.625 (magic number?) ... and have a funny square, box symbol in line

public void LinkUp() { if (_isLinkUp) return; _upTime = DateTime.Now; var span = _upTime.Subtract(_downTime); _downTimeLog.Add(new LinkDown() { _span = span, _status = _ipStatus, _time = _downTime }); _isLinkUp = true; } 

This is the bit that makes a magazine. _ipStatus - reason for ping failure (usually timeout).

  _downEventLog.AppendLine(" Duration-> " + linkDownLogEvent._span.TotalMilliseconds + "ms\n"); 

What is the bit that printing does ... Can anyone shed light on this apparent difference?

The answer to this question was given, but I will add a link here for more information.

EDIT:

It seems that the difference was not in the difference between Win7 and WinXP, but in 32-bit and 64-bit.

On 32-bit Windows systems like Henk , the granularity of the system clock is 15-16 ms, this is what gave me a value of 15.625 for each value less than 16 ms for a period of time.

In a 64-bit system, a system call is another set of methods that have much finer granularity. So, on my dev machine in x64, I had ms precision from the system clock!

Now the stopwatch uses a hardware interface using processor tools to record much finer details (maybe not for every processor, but I imagine something indecently accurate in accordance with this thinking). If the hardware underlying the OS does not have this level of instrumentation, it will, however, use system time. So be careful! But I would suggest that most modern desktops / laptops have this hardware ... There may not be built-in devices or such things, but then the stopwatch class is not in the Compact Framework, as far as I can see (here you should use QueryPerformanceCounter () )

Hope all this helps. It helped me a lot.

Somewhere near the _spanStopWatch initializer:

  if (!_spanStopWatch.IsHighResolution) { throw new ThisMachineIsNotAccurateEnoughForMyLikingException("Find a better machine."); } 

Nuts and bolts:

  public void LinkUp() { if (_isLinkUp) return; _spanStopWatch.Stop(); var span = _spanStopWatch.Elapsed; _downTimeLog.Add(new LinkDown() { _span = span, _status = _ipStatus, _time = _downTime }); _isLinkUp = true; } 
+6
source share
1 answer

0 or 15.625 (magic number?)

Yes, using DateTime.Now is accurate only for the length of the CPU timer, 15-20 ms depending on your hardware and OS version.

Use System.Diagnostics.Stopwatch for a more accurate time.

+5
source

Source: https://habr.com/ru/post/899680/


All Articles