English 中文(简体)
• 如何在互联网/C#中找到衡量精确度的时间范围?
原标题:How to get timestamp of tick precision in .NET / C#?
  • 时间:2009-09-12 21:08:38
  •  标签:

直到现在,我一直使用<代码>Datetime.Now来登出时间序列,但我注意到,如果你在座椅上印刷<代码>Datetime.Now,你就会看到,在离散的 app跳中,会增加。 页: 1 但是,对于我的申请中的某些假设情况,我需要尽可能准确,最好是精确(=100英亩)。 任何想法?

<>Update:

很明显,StopWatch/ 这是前进的道路,但只能用来衡量时间,所以我想到的是打电话<代码>。 现在,在申请开始后,即有<代码>StopWatch的操作,然后在<代码>StopWatch上添加了从<代码>上退还的初步数值。 至少应该给我准确的相对时间点,正确吗? 你们对这一点有何看法?

<>光>

<编码> StopWatch.ElapsedTicks is different from StopWatch.Elapsed.Ticks! 我使用了前者的假设标准1 = 100 ns,但在此情况下,标准1 = 1 / <代码>StopWatch.Fility/code>。 因此,采用与使用日期相当的标准<代码>。 StopWatch.Elapsed.Ticks。 我刚刚学到了这一艰难的道路。

<><>>>

我注意到,采用“制止枪支”做法,它随着时间的推移而消失。 大约10小时后,有5秒钟。 因此,我猜测,在X可能为1小时、30分钟、15分钟等的情况下,每X年就不得不对X进行反响。 我不相信,最好的反yn时机是什么,因为每 res都会改变可达20米的抵消。

最佳回答

每天24小时(>)的系统价值仅每15条或15条(或某些系统的10条)更新,因此,时间是按这些间隔计算的。 还有一个增量效应是,你的法典在多面的协调员中运行,因此,在你的申请不是“变数”,因此没有衡量目前的实际时间的情况下,你可以 stretch。

由于你重新寻找超时邮票价值(而不是单纯的时间为任意期限),<密码>类别本身不会做你所需要的。 我认为,你必须用一种<代码>Datetime/Stopwatch进行这种分析。 混合。 当申请开始时,你将储存目前的<代码>。 日期:UtcNow Value(即申请启动时的原油解决时间),然后开始<代码>Stopwatch 反对,如:

DateTime _starttime = DateTime.UtcNow;
Stopwatch _stopwatch = Stopwatch.StartNew();

然后,如果你需要高分辨率的<代码>/code>值,你会这样做:

DateTime highresDT = _starttime.AddTicks(_stopwatch.Elapsed.Ticks);

你们还可能希望定期重新开始——启动和进行监视,使由此产生的时间与系统时间相距甚远(尽管我不敢肯定会发生这种情况,而且需要很长的时间才能做到这一点)。

<>Update:由于从系统时间(每小时不超过半小时)的角度来看,“停止监视”do<><> > /em> 已从同系统时间(每小时不超过二分之一)中删除,我认为,根据要求检查时间之间的时间长度重新制定“混合代码<><>><>>><>>>>> 代码”类别是明智的:

public class HiResDateTime
{
    private static DateTime _startTime;
    private static Stopwatch _stopWatch = null;
    private static TimeSpan _maxIdle = 
        TimeSpan.FromSeconds(10);

    public static DateTime UtcNow
    {
        get
        {
            if ((_stopWatch == null) || 
                (_startTime.Add(_maxIdle) < DateTime.UtcNow))
            {
                Reset();
            }
            return _startTime.AddTicks(_stopWatch.Elapsed.Ticks);
        }
    }

    private static void Reset()
    {
        _startTime = DateTime.UtcNow;
        _stopWatch = Stopwatch.StartNew();
    }
}

如果你在某种定期间隔(每小时或每小时一次)重新启用混合时间,那么你就冒着在最后阅读时间之前将时间排回的风险,如小型日光储蓄时间问题。

问题回答

为了达到高分辨率计数,请使用静态停止观察。 • 方法:

long tickCount = System.Diagnostics.Stopwatch.GetTimestamp();
DateTime highResDateTime = new DateTime(tickCount);

只是看一看。 NET Source Code:

    public static long GetTimestamp() {
        if(IsHighResolution) {
            long timestamp = 0;    
            SafeNativeMethods.QueryPerformanceCounter(out timestamp);
            return timestamp;
        }
        else {
            return DateTime.UtcNow.Ticks;
        }   
    }

资料来源:

[接受的回答似乎并不安全,而且通过自己的承认,在时间上会倒退,造成重复的时间,从而造成这种替代答案]

如果你真正关心(你的评论)事实上是一个独特的时间档,按严格的加权顺序分配,并尽可能与系统时间相对应,你可以尝试这一替代做法:

public class HiResDateTime
{
   private static long lastTimeStamp = DateTime.UtcNow.Ticks;
   public static long UtcNowTicks
   {
       get
       {
           long orig, newval;
           do
           {
               orig = lastTimeStamp;
               long now = DateTime.UtcNow.Ticks;
               newval = Math.Max(now, orig + 1);
           } while (Interlocked.CompareExchange
                        (ref lastTimeStamp, newval, orig) != orig);

           return newval;
       }
   }
}

它确实退回了业务系统最准确的日期和时间。

操作系统还通过<代码>提供更高的分辨率。 QueryPerformanceCounter and QueryPerformanceFility/code> (Stopwatch. 这使你有时间间隔,但却没有给你一天的时间。 你也许会认为,这能够给你一个非常准确的时间和日子,但我不敢肯定,他们是如何在很长的间隔中玩弄的。

1). If you need high resolution absolute accuracy: you can t use DateTime.Now when it is based on a clock with a 15 ms interval (unless it is possible "slide" the phase).

Instead, an external source of better resolution absolute accuracy time (e.g. ntp), t1 below, could be combined with the high resolution timer (StopWatch / QueryPerformanceCounter).


页: 1 如果你需要高分辨率的话:

Sample DateTime.Now (t1) once together with a value from the high resolution timer (StopWatch / QueryPerformanceCounter) (tt0).

If the current value of the high resolution timer is tt then the current time, t, is:

t = t1 + (tt - tt0)

3). An alternative could be to disentangle absolute time and order of the financial events: one value for absolute time (15 ms resolution, possibly off by several minutes) and one value for the order (for example, incrementing a value by one each time and store that). The start value for the order can be based on some system global value or be derived from the absolute time when the application starts.

This solution would be more robust as it is not dependent on the underlying hardware implementation of the clocks/timers (that may vary betweens systems).

这太简单了。 在您的数据库中添加一个与贸易相关的时间。 之后,为了获得贸易令,就使用您的特性一栏,该栏应具有增益价值。

如果你在多个数据库中加入,并在事实发生后试图进行调节,那么,由于您的数据库时间略有差异,你将错判贸易令(如你所说,有7名实习生加薪)。

为了解决多个数据库问题,你可以发现每个行业打上订单的单一服务。 该服务可以返回一个日期。 UtcNow.Ticks and an incrementing trade number.

即便使用一种解决办法,凡从网络的一个地点进行交易且拥有更多的相对权,也可能首先(真实世界)进行贸易,但是由于相对权宜,这些交易按错误顺序进行。 由于这一原因,必须将贸易放在数据库中,而不是放在用户群。

d 在<>中添加以下内容: 回答 重提时间:

计算: 我用什么时间重提一下(_maxIdle,载于MusiGenesis 回答 s)

You know that with this solution you are not perfectly accurate, thats why you re-sync. But also what you implicitly want is the same thing as Ian Mercer solution s:

严格按订货顺序分配的独特时间档

因此,两个对等之间的时间(_maxIdle) 请打电话<>Synctime,以4条为功能:

  • the DateTime.UtcNow resolution
  • the ratio of accuracy you want
  • the precision level you want
  • An estimation of the out-of-sync ratio of your machine

显然,对这一变量的第一个限制是:

http://www.ohchr.org。

例如: 我急切希望我的准确性最差于0.5/hrs或1ms/day等。 (英文版:我不想错过0.5s/hrs=12s/day)。

因此,你不能比停止监视会给你提供的准确度。 这取决于您的同学率,这可能不是不变的。

另一项制约因素是两个类似情况之间的最低时间:

<>斯特凡>

这里的准确性和精确性是联系在一起的,因为如果你使用高精度(例如储存在非行)但精度较低,你可以打破<>。 Ian Mercer 声明,即严格的加价令。

Note: It seems DateTime.UtcNow may have a bigger default Res than 15ms (1ms on my machine) Follow the link: High accuracy DateTime.UtcNow

举一个例子:

上文评论的是非同学比率。

大约10小时后,有5秒钟。

我想要一点微小的精确性。 页: 1

逐点:

  • the DateTime.UtcNow resolution : 1ms
  • accuracy ratio >= out-of-sync ratio, lets take the most accurate possible so : accuracy ratio = out-of-sync ratio
  • the precision level you want : 1 microsec
  • An estimation of the out-of-sync ratio of your machine : 0.5s/hour (this is also my accuracy)

If you reset every 10s, imagine your at 9.999s, 1ms before reset. Here you make a call during this interval. The time your function will plot is ahead by : 0.5/3600*9.999s eq 1.39ms. You would display a time of 10.000390sec. After UtcNow tick, if you make a call within the 390micro sec, your will have a number inferior to the previous one. Its worse if this out-of-sync ratio is random depending on CPU Load or other things.

现在,我要说的是:Synctime,其最低价值和价值;我每1个ms。

采用同样的思路,我会把时间推到0.139微秒;低于我所希望的精度。 因此,如果我把这一职能称为9.999兆瓦,那么,在我重新开始之前,就会有9.999块土地。 之后,我就将规划10 000人。 我将有一个良好的秩序。

因此,其他制约因素是:精确度-平分×协同效应;精确度,可以肯定,因为数字可以四舍五入到-accuracy-ratio xix <精密度/2

这个问题已经解决。

www.un.org/Depts/DGACM/index_spanish.htm 因此,Quick recap将是:。

  • Retrieve your timer resolution.
  • Compute an estimate of the out-of-sync ratio.
  • accuracy ratio >= out-of-sync ratio estimate , Best accuracy = out-of-sync ratio
  • Choose your Precision Level considering the following:
  • timer-resolution <= SyncTime <= PrecisionLevel/(2*accuracy-ratio)
  • The best Precision you can achieve is timer-resolution*2*out-of-sync ratio

就上述比率(0.5/hr)而言,正确的协同时间为3.6万,这样四舍五入到3

以上比率和时间分辨率。 如果您希望达到一个标准精确度(0.1microsec),那么你需要一个不超过以下的集团外比率:180ms/hour

在对自己答复的最后答复中:

@Hermann: 我在最后两小时(没有重新定位)上进行了类似的测试,停监视器在2小时后只剩下约400个ms,因此sk本身似乎有变(但依然很严重)。 我感到惊讶的是,sk是这种坏事;我猜测,这就是为什么“停止监视”系统。 诊断。 • Musi Genes

So 停止监视精确度接近200米/小时,几乎是180米/小时。 是否有任何联系说明为什么我们的人数和这一数字如此接近? 不详。 但是,这种准确性足以使我们达到“加速”目标。

最佳精度: 例如,0.27微秒

然而,如果我在9.99万至9.99万之间多次提到,情况会发生。

2项职能要求可以结束,同时将同一时间序列退回,这两者的时间为9.999(因为我会看到更精确)。 绕过这一点,你不能触及精确程度,因为按照上述关系,它与Synctime有联系。 因此,你应执行<>Ian Mercer关于这些案件的解决办法。

请不要犹豫地评论我的回答。

如果需要时间计划来实施基准使用StopWatch,其精度比日期好得多。 现在。

我认为,这是解决这一问题的最佳途径:

long timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();




相关问题