32位和64位Windows之间的日期时间粒度 [英] Datetime Granularity between 32bit and 64bit Windows

查看:65
本文介绍了32位和64位Windows之间的日期时间粒度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为网络调试创建了一个小的工程应用程序.它获取IP地址列表并对其进行ping操作,以及用户设置的超时和速率.它记录平均往返时间,每次发送失败时,都会记录其持续时间和发生时间的时间戳...

I have create a little engineering app for some network debugging. It takes a list of IP addresses and pings them, with user set timeout and rate. It logs the average round-trip time and every time one of the sends fails it logs it's duration of failure and a timestamp of when it happened...

就是这个想法.我在装有.Net4的Win7机器上开发了它,不得不将其放置在一组XP笔记本电脑上.

That's the idea. I developed it on a Win7 machine with .Net4 and have had to put the thing on a set of XP laptops.

问题是测试期间我的盒子上的持续时间值显示了不错的ms持续时间,在XP盒子上,当我看到它们的持续时间值显示为0或15.625(幻数?)...并在字符串中带有有趣的正方形,盒子符号时?

The problem is the duration values on my box during testing show nice ms durations, on the XP boxes when I look they show 0 or 15.625 (magic number?)... and have the funny square, box symbol in the string?

    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;
    }

这就是做日志的部分._ipStatus是ping失败的原因(通常是超时).

That's the bit that does the log. The _ipStatus is the ping failure reason (typically timeout).

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

这就是打印的要点...有人可以阐明这一明显的区别吗?

That's the bit that does the print... Can anyone shed any light on this apparent difference?

问题已经回答,但我将在此处进行编辑以获取更多信息.

The question has been answered but I will include an edit here for some more info.

似乎差异不是归因于Win7和WinXP差异,而是32位和64位.

It seems that the difference was down not to the Win7 and WinXP difference but 32bit and 64bit.

Henk 指出的32位Windows系统中,系统时钟的粒度为15-16ms,对于每个小于16ms的时间值,这就是给我带来15.625的值的原因.

In a 32 bit windows systems as Henk points out, the granularity of the system clock is 15-16ms, this is what gave me the value of 15.625 for every value less than 16ms for the timespan.

在64位系统中,系统调用的是一组具有更精细粒度的不同方法.因此,在x64的开发机上,我的系统时钟具有ms精度!

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

现在,秒表通过处理器工具使用硬件接口来记录更精细的粒度(可能不是每个处理器的滴答声,但我想这与这种想法相符.如果操作系统底层的硬件没有此级别的检测,则它将占用系统时间.所以要当心!但是我想大多数现代台式机/笔记本电脑都具有这种工具...嵌入式设备或类似性质的东西可能没有,但是就我所知,秒表类不在Compact Framework中(在这里,您必须使用QueryPerformanceCounter()).

Now, the stopwatch uses a hardware interface via the processor instrumentation to record a much finer granularity (probably not every processor tick, but I imagine something obscenely accurate in-line with this thinking). If the hardware underlying the OS does not have this level of instrumentation, it will however use the system time. So beware! But I would guess most modern desktops/laptops have this instrumentation... Embedded devices or things of that nature might not, but then the stopwatch class is not in the Compact Framework as far as I can see (here you have to use QueryPerformanceCounter()).

希望所有这些都对您有所帮助.这对我有很大帮助.

Hope all this helps. It's helped me a lot.

_spanStopWatch初始化程序周围的某处:

Somewhere around the _spanStopWatch initialiser:

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

基本要点:

    public void LinkUp()
    {
        if (_isLinkUp) return;

        _spanStopWatch.Stop();
        var span = _spanStopWatch.Elapsed;
        _downTimeLog.Add(new LinkDown()
                            {
                                _span = span,
                                _status = _ipStatus,
                                _time = _downTime
                            });
        _isLinkUp = true;
    }

推荐答案

0或15.625(幻数?)

0 or 15.625 (magic number?)

是的,使用 DateTime.Now 仅精确到CPU时间片的长度(15-20毫秒,具体取决于您的硬件和操作系统版本).

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

使用 System.Diagnostics.Stopwatch 以获得更准确的计时.

Use System.Diagnostics.Stopwatch for more accurate timing.

这篇关于32位和64位Windows之间的日期时间粒度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆