Bạn có thể sử dụng this DateTimePrecise class để có được một thời gian chính xác cao trong .NET
CẬP NHẬT
Liên kết CodeProject không còn làm việc. Tôi đã lấy mã số from archive.org và nhúng mã vào đây để tham khảo trong tương lai. Mã này được bao gồm ở đây "như là", chính xác cách nó được đưa vào trang CodeProject.
DateTimePrecise
là dễ dàng để sử dụng như DateTime.Now
, ngoại trừ việc DateTimePrecise.Now
là một phương pháp dụ thay vì một phương pháp tĩnh, vì vậy trước tiên bạn phải tạo một 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;
}
Nguồn
2010-06-29 13:06:00
Bạn đang nói về các ngắt hạt nhân? http://lwn.net/Articles/145973/ – Nitrodist
Nitrodist: Vâng, đó là giải thích về cùng một vấn đề. – Gabe
Anh ấy đang nói về việc đánh dấu hạt nhân, một dấu tích hạt nhân là một phần cứng được lập lịch trình (CPU) 'ngắt quãng thời gian'. Tuy nhiên, Windows vẫn chưa có hiệu quả về điện năng (hiệu suất kém hơn 50% so với Android, và ít hơn 20% so với hệ điều hành Mac), nhưng không có lý do gì khiến nó trở nên tồi tệ hơn. Các dấu tick của hạt nhân được sử dụng để gọi các callback thời gian đã hết hạn (hoặc coalesced), cập nhật lại luồng/lập lịch trình quy trình và tăng số lượng đánh dấu hệ điều hành toàn cầu. –