Windows系统时间,精度为毫秒

时间:2010-06-29 13:00:26

标签: c# .net windows datetime

Related to my previous question,但是使用C#,我需要精确的系统时间,包括毫秒。

C#时间函数的准确度高达10到15毫秒,但不完全是1毫秒。

Queue性能计数器的情况也是如此。有没有其他方法可以将精度提高到精确到毫秒?

3 个答案:

答案 0 :(得分:8)

您可以使用this DateTimePrecise class在.NET中获得高准​​确度的时间

<强>更新
CodeProject链接不再有效。我已经提取代码from archive.org并将其嵌入此处以供将来参考。此代码“按原样”包含在此处,与CodeProject页面中包含的方式完全相同。

  

DateTimePreciseDateTime.Now一样易于使用,除了DateTimePrecise.Now是实例方法而不是静态方法,因此您必须首先实例化DateTimePrecise

using System.Diagnostics;

/// DateTimePrecise provides a way to get a DateTime that exhibits the
/// relative precision of
/// System.Diagnostics.Stopwatch, and the absolute accuracy of DateTime.Now.
public class DateTimePrecise
{
    /// Creates a new instance of DateTimePrecise.
    /// A large value of synchronizePeriodSeconds may cause arithmetic overthrow
    /// exceptions to be thrown. A small value may cause the time to be unstable.
    /// A good value is 10.
    /// synchronizePeriodSeconds = The number of seconds after which the
    /// DateTimePrecise will synchronize itself with the system clock.
    public DateTimePrecise(long synchronizePeriodSeconds)
    {
        Stopwatch = Stopwatch.StartNew();
        this.Stopwatch.Start();

        DateTime t = DateTime.UtcNow;
        _immutable = new DateTimePreciseSafeImmutable(t, t, Stopwatch.ElapsedTicks,
            Stopwatch.Frequency);

        _synchronizePeriodSeconds = synchronizePeriodSeconds;
        _synchronizePeriodStopwatchTicks = synchronizePeriodSeconds *
            Stopwatch.Frequency;
        _synchronizePeriodClockTicks = synchronizePeriodSeconds *
            _clockTickFrequency;
    }

    /// Returns the current date and time, just like DateTime.UtcNow.
    public DateTime UtcNow
    {
        get
        {
            long s = this.Stopwatch.ElapsedTicks;
            DateTimePreciseSafeImmutable immutable = _immutable;

            if (s < immutable._s_observed + _synchronizePeriodStopwatchTicks)
            {
                return immutable._t_base.AddTicks(((
                    s - immutable._s_observed) * _clockTickFrequency) / (
                    immutable._stopWatchFrequency));
            }
            else
            {
                DateTime t = DateTime.UtcNow;

                DateTime t_base_new = immutable._t_base.AddTicks(((
                    s - immutable._s_observed) * _clockTickFrequency) / (
                    immutable._stopWatchFrequency));

                _immutable = new DateTimePreciseSafeImmutable(
                    t,
                    t_base_new,
                    s,
                    ((s - immutable._s_observed) * _clockTickFrequency * 2)
                    /
                    (t.Ticks - immutable._t_observed.Ticks + t.Ticks +
                        t.Ticks - t_base_new.Ticks - immutable._t_observed.Ticks)
                );

                return t_base_new;
            }
        }
    }

    /// Returns the current date and time, just like DateTime.Now.
    public DateTime Now
    {
        get
        {
            return this.UtcNow.ToLocalTime();
        }
    }

    /// The internal System.Diagnostics.Stopwatch used by this instance.
    public Stopwatch Stopwatch;

    private long _synchronizePeriodStopwatchTicks;
    private long _synchronizePeriodSeconds;
    private long _synchronizePeriodClockTicks;
    private const long _clockTickFrequency = 10000000;
    private DateTimePreciseSafeImmutable _immutable;
}

internal sealed class DateTimePreciseSafeImmutable
{
    internal DateTimePreciseSafeImmutable(DateTime t_observed, DateTime t_base,
         long s_observed, long stopWatchFrequency)
    {
        _t_observed = t_observed;
        _t_base = t_base;
        _s_observed = s_observed;
        _stopWatchFrequency = stopWatchFrequency;
    }
    internal readonly DateTime _t_observed;
    internal readonly DateTime _t_base;
    internal readonly long _s_observed;
    internal readonly long _stopWatchFrequency;
}

答案 1 :(得分:7)

Windows不希望通过每秒更新1000次系统时钟来浪费电力,因此默认情况下每秒只更新60-100次。如果将多媒体定时器设置为1ms,则可以从时钟获得1ms的分辨率,但不建议这样做。

为了详细说明节电情况,当CPU空闲一段时间后会发生的事情是它可以进入一个非常低功耗的状态。每当它被中断(例如,增加时钟滴答)时,它必须离开其非常低功率状态并使用大量电力为整个CPU供电以服务该中断。换句话说,额外的功率不在于增加时钟滴答,而是让CPU保持清醒状态。

由于我的笔记本电脑在时钟频率为60Hz时空闲时使用10W,而在1000Hz时使用11W,我的电池续航时间为300分钟,这个较慢的时钟给了我近30分钟的电池续航时间!

答案 2 :(得分:6)

尝试使用System.Diagnostics.Stopwatch进行高分辨率计时。

  

如果安装了硬件和   操作系统支持   高分辨率性能计数器,   然后秒表类使用它   用于衡量经过时间的计数器。   否则,秒表类使用   用于测量经过的系统计时器   时间。

尝试原生DateTime.Ticks,系统时间精度可达100纳秒; 1毫秒= 10000个滴答。

while (true)
{
    System.Threading.Thread.Sleep(1);

    Console.WriteLine("{0} {1}",
        System.DateTime.Now.Ticks,
        System.DateTime.Now.ToString("ss:fff"));
}

PS > .\test.exe
    634134152924322129 52:432
    634134152924332129 52:433
    634134152924342130 52:434
    634134152924352130 52:435
    634134152924362131 52:436
    634134152924372131 52:437
    634134152924382132 52:438
    634134152924392133 52:439
    634134152924402133 52:440
    634134152924412134 52:441
    634134152924422134 52:442
    634134152924432135 52:443