AverageTimer32计数器值变为零 [英] AverageTimer32 counter value becomes zero

查看:135
本文介绍了AverageTimer32计数器值变为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一张code这样如下:

I have written a piece of code like this below:

有关测试我调用方法ComputeAndLog和性能监视器,我可以看到非零平均值。但是当我结束我的测试中,平均性能计数器值下降到零。任何想法,为什么多数民众赞成的情况下?

For testing i call the method ComputeAndLog and in the performance monitor i can see the non-zero average value. However as soon as i end my testing, the average performance counter value drops to zero. Any idea why thats the case ?

也许我用错了柜台?

要求我的是,我有一个功能,我必须来计算,平均而言,有多少时间函数需要完成。喜欢的东西如下:

The requirement I have is that I have a function and i have to calculate that on average, how much time that function takes to complete. Something like below:

void ComputeAndLog()
{
    Stopwatch stopWatch = Stopwatch.StartNew();
    FunctionWhoseAveragetTimeIsToBeMeasured();
    write_counter(stopWatch.ElapsedTicks);
}

void write_counter(long timeForCompletion)
{
    averageTimeCounter.IncrementBy(timeForCompletion);
    averageBaseCounter.Increment();
}

谢谢 XOXO

Thanks xoxo

推荐答案

在AverageTimer32 / 64不计算在执行的所有测量值的平均值。相反,它提供了你的尺寸的比值来衡量你操作的数量。

The AverageTimer32/64 does not calculate the average of all measurements you perform. Instead it provides the ration of your measurements to the number of operations you measured.

基本上,这个问题与你的code是您使用的是一个新的计时器每次执行测量时间的事实。

Basically, the issue with your code is the fact you are using a new timer every time you perform a measurement.

要了解AverageTimer是如何工作的,它可能有助于了解其背后的公式。这也回答了为什么人们需要一个AverageBase使用AverageTimer。

To understand how the AverageTimer works, it might be helpful to understand the formula behind it. This also answers why one needs an AverageBase to use an AverageTimer.

该公式AverageTimer方法如下:

The formula for the AverageTimer is as following:

((N1 - N0) / F) / (B1 - B0)

  • 在N1的电流读数在时间t(AverageTimer)
  • N0阅读之前,在t - 1(AverageTimer)
  • 在t B1当前的计数器(AverageBase)
  • 在B0专柜前,在t - 1(平均基本)
  • F因子计算蜱/秒

在果壳中的表现公式取当前时间刻度和减去previous之一。结果由系数F分给你的时候,你的操作运行,因为在t-1拍摄的最后一次测量。

In a nutshell the formular takes the current time in ticks and subtracts the previous one. The result divided by the factor F gives you the time you operation run since the last measurement taken at t-1.

现在你当前计数器减柜台前分裂这一点。这可能是通常是一个。因此,你有你的操作的一个测量的平均时间。

Now you divide this by the current counter minus the counter before. This might be usually one. As a result you have the average time of your operation for one measurement.

现在使用AverageBase你可以跳过各个测量点。想在这里你可以设置计数器只执行每十个操作的情况。由于您的最后一次测量,你会递增AverageTimer由新的时间测量全部十个操作,但十递增AverageBase。最后,您将收到的平均时间为一个操作(即使你已经测量了所有十个操作调用)。

Using the AverageBase you now can step over various measurement points. Think of a case where you can set the counter only every tenth operation you perform. Since your last measurement you would increment the AverageTimer by the new time measurement for all ten operations but increment the AverageBase by ten. Eventually, you will receive the average time for one operation (even if you have measured over all ten operation calls).

看你的code比如你发送总是差从定时器开始计时结束。让这成为一系列数字,像10,9,8,7,6,同时提高AverageBase 1。

Looking at your code example you send in always the difference from timer start to timer end. Let this be a series of numbers like 10, 9, 8, 7 ,6 while increasing the AverageBase by 1.

有关,你知道会收到以下结果第二次测量:

For the second measurement you know will receive the following result:

(9 - 10)/ F /(1 - 0)= -1 / F / 1

(9 - 10) / F / (1 - 0) = -1 / F / 1

使用f为1为简单起见,你会得到-1的结果。

With F being 1 for simplicity you will get -1 as result.

正确的价值观却提出应

10,19,27,34,40

10, 19, 27, 34, 40

再次同一个例子,我们将获得

Again the same example we will get

(19 - 10)/ F /(1 - 0)= 9 / F / 1

(19 - 10) / F / (1 - 0) = 9 / F / 1

再次使用F为1,你将有9的平均时间为您的操作。 正如你所看到的,所测量的下一个值必须大于previous一个,这样的AverageTimer正常工作。

Again, with F being 1 you will have an average time of 9 for your operation. As you can see, the next value measured needs to be greater than the previous one so the AverageTimer works properly.

在您的例子中,你可以使用一个全球性的秒表。而不是从它的新的,使用的start()(不重新启动())。如上所见,计数器将计算内部的时间差。这样,你会得到正确的测量。

In your example you might use a global Stopwatch. Instead of starting it new, use Start() (not Restart()). As seen above, the counter will calculate the difference in time internally. That way you will get correct measurements.

到零也是情理之中,一旦你完成了你的测试或者你节目结束时,计数器可能会被关闭,不提供任何价值了。您可以通过调用在柜台上的Close()方法做手工。

Going to zero also makes sense, once you are done with your testing or your program ends, the counter will probably be closed and not provide any values anymore. You can do it manually by calling the Close() method on the counter.

这篇关于AverageTimer32计数器值变为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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