ASP.NET 页面中的时间测量

发布于 2024-08-09 00:52:49 字数 1210 浏览 3 评论 0原文

我有一个普通的 asp.net 页面,其中包含一些我想要测量其执行时间的代码。为此,我创建了一个非常基本的帮助器类:

public class Timing
{
    private long m_ticksBefore;
    private long m_ticksAfter;

    public void Before()
    {
        m_ticksBefore = DateTime.Now.Ticks;
    }

    public void After()
    {
        m_ticksAfter = DateTime.Now.Ticks;
    }

    public void TraceTime(string note)
    {
        TimeSpan span = new TimeSpan(m_ticksAfter - m_ticksBefore);
        System.Diagnostics.Trace.Write(string.Format("\n...Timing for {0}...\nTicks: \t{1}\nMilliseconds: \t{2}\nSeconds: \t{3}\n..................", note, span.Ticks, span.TotalMilliseconds, span.TotalSeconds));
    }
}

我实例化一个新的 Timing 对象,并使用 Before() 和 After 方法包​​装我希望监视的代码,最后调用 TraceTime() 方法以将结果输出到IDE 中的输出窗口。

奇怪的是,对页面的第一个请求导致预期的计时结果约为 40 毫秒,但刷新页面 (F5) 却给了我 0 个滴答的执行时间。即使使用新参数执行页面,强制在监视区域内执行不同的代码,也会显示出平坦的 0。

如果我使用 System.Diagnostics.StopWatch,我会得到更真实的结果

Stopwatch watch1 = Stopwatch.StartNew();
//Some code to monitor
watch1.Stop();
System.Diagnostics.Trace.Write(watch1.ElapsedTicks);

对我来说,代码看起来非常相似,但我似乎找不到为什么第一种方法无法输出真实的结果......

这似乎是在幕后执行的某种优化问题,但我并没有真正明白。也许是由于我个人的时间问题,即就寝时间问题......

I have a normal asp.net page containing some code that I want to measure the execution time of. I have created a very basic helper class for this purpose:

public class Timing
{
    private long m_ticksBefore;
    private long m_ticksAfter;

    public void Before()
    {
        m_ticksBefore = DateTime.Now.Ticks;
    }

    public void After()
    {
        m_ticksAfter = DateTime.Now.Ticks;
    }

    public void TraceTime(string note)
    {
        TimeSpan span = new TimeSpan(m_ticksAfter - m_ticksBefore);
        System.Diagnostics.Trace.Write(string.Format("\n...Timing for {0}...\nTicks: \t{1}\nMilliseconds: \t{2}\nSeconds: \t{3}\n..................", note, span.Ticks, span.TotalMilliseconds, span.TotalSeconds));
    }
}

I instantiate a new Timing object and wrap the code I wish to monitor with the Before() and After methods and finally call the TraceTime() method in order to output the result to the output window in the IDE.

The strange thing is that the first request to the page results in an expected timing result of some 40 milliseconds, but refreshing the page (F5) gives me 0 ticks of execution time. Even executing the page with new parameters forcing the execution of different code within the monitored area reveals a flat 0.

If I use System.Diagnostics.StopWatch instead I get much more realistic results

Stopwatch watch1 = Stopwatch.StartNew();
//Some code to monitor
watch1.Stop();
System.Diagnostics.Trace.Write(watch1.ElapsedTicks);

To me the code seems very similar and I cannot seem to find out why the first approach fails to output realistic results...

It seems to be some sort of optimization issue that is performed behind the scene, but I don't really get it. Maybe it is due to my personal Timing issue i.e. bed-time issue...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2024-08-16 00:52:49

尽管 DateTime.Now 看起来具有 100ns 的分辨率,但 Windows 实际上仅每 15ms 更新一次其内部时钟。因此,如果您的代码运行时间少于 7.5 毫秒,则看起来根本没有花费任何时间,因为平均而言,您必须等待 7.5 毫秒才能更改 DateTime.Now。

如果您需要更准确的计时,您可以(正如您所发现的)使用秒表。这使用不同的、更准确的 API 来获取结果。

编辑 DateTime 的 MSDN 文档。现在给出的分辨率为“大约10ms”。 GetTickCount API(使用相同的系统计时器)给出的分辨率为“通常在 10 毫秒到 16 毫秒范围内”。

显然,它取决于硬件 - 单处理器系统通常为 10 毫秒,多处理器系统通常为 15 毫秒。

Although DateTime.Now appears to have a resolution of 100ns, Windows actually only updates its internal clock every 15ms. Therefore, if your code takes less than about 7.5ms to run, it will appear to have taken no time at all, since on average you have to wait 7.5ms for DateTime.Now to change.

If you need more accurate timing, you can (as you've found) use StopWatch. This uses a different, more accurate API to get results.

EDIT The MSDN documentation for DateTime.Now gives the resolution as "approximately 10ms". The GetTickCount API (which uses the same system timer) gives its resolution as "typically in the range of 10 milliseconds to 16 milliseconds".

Apparently it's hardware dependent - single processor systems are typically 10ms, multiple processor systems are typically 15ms.

獨角戲 2024-08-16 00:52:49

我没有遇到和你一样的问题。
我将以下代码放在按钮单击事件上:

Timing timing = new Timing();
timing.Before();
for (int i = 0; i < 100; i++)
    Thread.Sleep(1);
timing.After();
// I altered TraceTime to receive a HttpResponse.
timing.TraceTime("this is a note", this.Response);

响应始终接近:

...此时间是一个注释...刻度:
1000057 毫秒:100,0057
秒:0,1000057 ......................

无论是否回发,不回发...如果我刷新页面或不刷新页面。

您如何使用 Timing 类?

编辑

我还将上面的代码放在页面加载处理程序上,并得到了完全相同的结果。无论是回发、刷新还是其他什么都没关系。

您必须告诉我们错误发生的上下文。

I'm not experiencing the same problem as you are.
I put the following code on a button click event:

Timing timing = new Timing();
timing.Before();
for (int i = 0; i < 100; i++)
    Thread.Sleep(1);
timing.After();
// I altered TraceTime to receive a HttpResponse.
timing.TraceTime("this is a note", this.Response);

The response is always close to:

...Timing for this is a note... Ticks:
1000057 Milliseconds: 100,0057
Seconds: 0,1000057 ..................

No matter if it's a post back, not post back... If I refreshed the page or not.

How are you using the Timing class?

Edit

I also put the code above on the Page Load handler and got the exact same results. It doesn't matter if it's a post-back, refresh or whatever.

You have to tell us the context in which the error is happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文