测量计时器的精度(例如Stopwatch/QueryPerformanceCounter) [英] Measure precision of timer (e.g. Stopwatch/QueryPerformanceCounter)

查看:264
本文介绍了测量计时器的精度(例如Stopwatch/QueryPerformanceCounter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到C#中的Stopwatch类可以在其下面使用类似三个不同的计时器之类的东西

Given that the Stopwatch class in C# can use something like three different timers underneath e.g.

  • 系统计时器,例如大约+-10 ms的精度取决于可以使用
  • System timer e.g. precision of approx +-10 ms depending on timer resolution that can be set with timeBeginPeriod it can be approx +-1 ms.
  • Time Stamp Counter (TSC) e.g. with a tick frequency of 2.5MHz or 1 tick = 400 ns so ideally a precision of that.
  • High Precision Event Timer (HPET) e.g. with a tick frequency of 25MHz or 1 tick = 40 ns so ideally a precision of that.

我们如何测量其可观察的精度?精度定义为

how can we measure the observable precision of this? Precision being defined as

精度是指两次或更多次测量的接近度 其他.

Precision refers to the closeness of two or more measurements to each other.

现在,如果Stopwatch使用HPET,这是否意味着我们可以使用Stopwatch获得与计时器频率等效的精度测量值?

Now if the Stopwatch uses HPET does this mean we can use Stopwatch to get measurements of a precision equivalent to the frequency of the timer?

我不这么认为,因为这要求我们能够使用零偏差或完全固定的开销的计时器,据我所知,这对于Stopwatch是不正确的.例如,在使用HPET并调用时:

I don't think so, since this requires us to be able to use the timer with zero variance or a completely fixed overhead, which as far as I can tell is not true for Stopwatch. For example, when using HPET and calling:

var before_ticks = Stopwatch.GetTimestamp();
var after_ticks = Stopwatch.GetTimestamp();
var diff_ticks = after_ticks - before_ticks;

然后差异将表示为100 ticks4000 ns,并且差异也将有所变化.

then the diff will be say approx 100 ticks or 4000 ns and it will have some variance too.

那么一个人怎么可以通过实验来测量Stopwatch的可观察精度呢?因此,它支持下面所有可能的计时器模式.

So how could one experimentally measure the observable precision of the Stopwatch? So it supports all possible timer modes underneath.

我的想法是搜索最小滴答数!= 0,首先建立Stopwatch滴答声的开销,对于系统计时器而言,它一直是0,直到例如10ms为10 * 1000 * 10 = 100,000个滴答,因为系统计时器的滴答分辨率为100ns,但精度远非如此.对于HPET,它永远不会为0,因为调用Stopwatch.GetTimestamp()的开销高于计时器的频率.

My idea would be to search for the minimum number of ticks != 0, to first establish the overhead in ticks of the Stopwatch that is for system timer this would be 0 until e.g. 10ms which is 10 * 1000 * 10 = 100,000 ticks since system timer has a tick resolution of 100ns, but the precision is far from this. For HPET it will never be 0 since the overhead of calling Stopwatch.GetTimestamp() is higher than the frequency of the timer.

但这并没有说明我们可以使用计时器测量的精度.我的定义是,我们可以可靠地测量的滴答差异有多小.

But this says nothing about how precise we can measure using the timer. My definition would be how small a difference in ticks we can measure reliably.

可以通过测量不同的迭代次数ala来执行搜索:

The search could be performed by measuring different number of iterations ala:

var before = Stopwatch.GetTimestamp();
for (int i = 0; i < iterations; ++i)
{
    action(); // Calling a no-op delegate Action since this cannot be inlined
}
var after = Stopwatch.GetTimestamp();

首先可以找到一个下界,对于给定数量的iterations,说全部10次测量都会产生非零的滴答声,并将这些测量值保存在long ticksLower[10]中.然后,产生滴答差的最接近的可能迭代次数总是高于可以找到的前10个测量值中的任何一个,将其保存在long ticksUpper[10]中.

First a lower bound could be found where all of say 10 of measurements for a given number of iterations yield a non-zero number of ticks, save these measurements in long ticksLower[10]. Then the closest possible number of iterations that yield tick difference that is always higher that any of the first 10 measurements could be found, save these in long ticksUpper[10].

最坏情况的精度将是ticksUpper中的最高价位减去ticksLower中的最低价位.

Worst case precision would then be the highest ticks in ticksUpper minus lowest ticks in ticksLower.

听起来合理吗?

为什么我想知道Stopwatch的可观察精度?因为这可以用于确定时间长度,所以您需要进行测量才能获得一定水平的微基准测量精度. IE.对于3位精度,长度应大于计时器精度的1000倍.当然,在此长度下,会多次测量.

Why do I want to know the observable precision of the Stopwatch? Because this can be used for determining the length of time you would need to measure for to get a certain level of precision of micro-benchmarking measurements. I.e. for 3 digit precision the length should be >1000 times the precision of the timer. Of course, one would measure multiple times with this length.

推荐答案

秒表类公开了

The Stopwatch class exposes a Frequency property that is the direct result of calling SafeNativeMethods.QueryPerformanceFrequency. Here is an excerpt of the property page:

频率值取决于基础时序的分辨率 机制.如果已安装的硬件和操作系统支持 高分辨率性能计数器,则频率"值反映 该计数器的频率.否则,频率值将基于 在系统计时器频率上.

The Frequency value depends on the resolution of the underlying timing mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Frequency value reflects the frequency of that counter. Otherwise, the Frequency value is based on the system timer frequency.

这篇关于测量计时器的精度(例如Stopwatch/QueryPerformanceCounter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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