在C#中的措施执行时间 [英] Measure execution time in C#

查看:161
本文介绍了在C#中的措施执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想衡量一段代码的执行,我想知道什么是最好的方法来做到这一点。

I want to measure the execution of a piece of code and I'm wondering what the best method to do this is?

选项1:

DateTime StartTime = DateTime.Now;

//Code

TimeSpan ts = DateTime.Now.Subtract(StartTime);
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, "RunTime");



选项2:$ B $使用System.Diagnostics程序B:

Option 2: using System.Diagnostics;

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    //Code

    stopWatch.Stop();
    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value.
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
    Console.WriteLine(elapsedTime, "RunTime");

这是不是简单地为标杆,它的实际应用程序的一部分。该功能将要执行的时间是相关数据。但是,它并不需要是原子或超准确。

This isn't simply for benchmarking, its actually part of the application. The time the function takes to execute is relevant data. It doesn't however need to be atomic or hyper-accurate.

哪个选项是用于生产代码更好,或者做其他任何人使用不同的东西,也许更好?

Which option is better for production code, or does anybody else use something different and perhaps better?

推荐答案

秒表类是专门设计用来测量经过时间,可能(如果适用于您的硬件)提供了使用基本的高频硬件定时器良好粒度/精度。因此,这似乎是最好的选择。

The Stopwatch class is specifically designed to measure elapsed time and may (if available on your hardware) provide good granularity/accuracy using an underlying high-frequency hardware timer. So this seem the best choice.

可以被用来确定高分辨率定时是否可用IsHighResolution属性。每文档,这个类提供的'最好的'的Win32 API,用于精确定时的包装:

The IsHighResolution property can be used to determine whether high resolution timing is available. Per the documentation, this class offers a wrapper on the 'best available' Win32 APIs for accurate timing:

具体而言,频率字段和
GetTimestamp 方法可以在
座非托管的Win32 API的
中使用 QueryPerformanceFrequency的
QueryPerformanceCounter的

Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.

有是这些Win32 API的[这里],并在联MSDN文档的 2

There is detailed background on those Win32 APIs [here] and in linked MSDN docs 2.

的高分辨率定时器

一个计数器是在
编程用来指一个
递增变量的总称。一些系统
包括一个高分辨率性能
计数器提供高分辨率
经过时间

A counter is a general term used in programming to refer to an incrementing variable. Some systems include a high-resolution performance counter that provides high-resolution elapsed times.

如果高分辨率性能在系统上存在
计数器,可以
使用的 QueryPerformanceFrequency的
功能来表达的频率,以每秒
计数。的
count的值是处理器依赖性。在某些
的处理器,例如,计数
可能是
处理器时钟的周期率。

If a high-resolution performance counter exists on the system, you can use the QueryPerformanceFrequency function to express the frequency, in counts per second. The value of the count is processor dependent. On some processors, for example, the count might be the cycle rate of the processor clock.

QueryPerformanceCounter的的功能
检索
高分辨率性能计数器的当前值。
。使用在
开始和结束的
代码段的调用此函数,应用程序基本上使用
中的计数器作为高分辨率
定时器。例如,假设
QueryPerformanceFrequency的的表示

高分辨率性能计数器的频率是
每秒50000计数。如果
应用程序调用
QueryPerformanceCounter的的马上
之前,立即进行定时的代码
节之后,
计数器的值可能是1500计数分别为
和3500计数。这些
值将表明,虽然代码
执行0.04秒
(2000个计数)结束。

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter. By calling this function at the beginning and end of a section of code, an application essentially uses the counter as a high-resolution timer. For example, suppose that QueryPerformanceFrequency indicates that the frequency of the high-resolution performance counter is 50,000 counts per second. If the application calls QueryPerformanceCounter immediately before and immediately after the section of code to be timed, the counter values might be 1500 counts and 3500 counts, respectively. These values would indicate that .04 seconds (2000 counts) elapsed while the code executed.

这篇关于在C#中的措施执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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