计算方法的执行时间

本文翻译自:Calculate the execution time of a method

Possible Duplicate: 可能重复:
How do I measure how long a function is running? 如何测量函数运行的时间?

I have an I/O time-taking method which copies data from a location to another. 我有一个I / O计时方法,可以将数据从一个位置复制到另一个位置。 What's the best and most real way of calculating the execution time? 什么是计算执行时间的最佳和最实际的方法? Thread ? Thread Timer ? Timer Stopwatch ? Stopwatch Any other solution? 还有其他方法吗? I want the most exact one, and briefest as much as possible. 我想要最精确的一个,尽可能简短。


#1楼

参考:https://stackoom.com/question/wp78/计算方法的执行时间


#2楼

Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET. Stopwatch专为此目的而设计,是衡量.NET中时间执行的最佳方法之一。

var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;

Do not use DateTime to measure time execution in .NET. 不要使用DateTime来测量.NET中的时间执行。


UPDATE: 更新:

As pointed out by @series0ne in the comments section: If you want a real precise measurement of the execution of some code, you will have to use the performance counters that's built into the operating system. 正如@ series0ne在评论部分所指出的:如果您想要对某些代码的执行进行真正精确的测量,则必须使用内置于操作系统中的性能计数器。 The following answer contains a nice overview. 以下答案包含一个很好的概述。


#3楼

If you are interested in understand performance, the best answer is to use a profiler. 如果您有兴趣了解性能,最好的答案是使用分析器。

Otherwise, System.Diagnostics.StopWatch provides a high resolution timer. 否则, System.Diagnostics.StopWatch提供高分辨率计时器。


#4楼

StopWatch class looks for your best solution. StopWatch类寻找最佳解决方案。

Stopwatch sw = Stopwatch.StartNew();
DoSomeWork();
sw.Stop();

Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

Also it has a static field called Stopwatch.IsHighResolution . 它还有一个名为Stopwatch.IsHighResolution的静态字段。 Of course, this is a hardware and operating system issue. 当然,这是硬件和操作系统问题。

Indicates whether the timer is based on a high-resolution performance counter. 指示计时器是否基于高分辨率性能计数器。


#5楼

StopWatch will use the high-resolution counter StopWatch将使用高分辨率计数器

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. 秒表通过计算基础计时器机制中的计时器滴答来测量经过的时间。 If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. 如果安装的硬件和操作系统支持高分辨率性能计数器,则Stopwatch类使用该计数器来测量经过的时间。 Otherwise, the Stopwatch class uses the system timer to measure elapsed time. 否则,Stopwatch类使用系统计时器来测量经过的时间。 Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation. 使用Frequency和IsHighResolution字段确定秒表计时实施的精度和分辨率。

If you're measuring IO then your figures will likely be impacted by external events, and I would worry so much re. 如果您正在测量IO,那么您的数据可能会受到外部事件的影响,我会非常担心。 exactness (as you've indicated above). 准确性 (如上所述)。 Instead I'd take a range of measurements and consider the mean and distribution of those figures. 相反,我会进行一系列测量,并考虑这些数字的平均值和分布。


#6楼

From personal experience, the System.Diagnostics.Stopwatch class can be used to measure the execution time of a method, however, BEWARE : It is not entirely accurate! 从个人的经验, System.Diagnostics.Stopwatch类可用于测量方法的执行时间,但要注意 :这是不完全准确!

Consider the following example: 请考虑以下示例:

Stopwatch sw;

for(int index = 0; index < 10; index++)
{
    sw = Stopwatch.StartNew();
    DoSomething();
    Console.WriteLine(sw.ElapsedMilliseconds);
}

sw.Stop();

Example results 示例结果

132ms
4ms
3ms
3ms
2ms
3ms
34ms
2ms
1ms
1ms

Now you're wondering; 现在你想知道; "well why did it take 132ms the first time, and significantly less the rest of the time?" “那么为什么第一次需要132毫秒,其余时间显着减少?”

The answer is that Stopwatch does not compensate for "background noise" activity in .NET, such as JITing. 答案是Stopwatch不能补偿.NET中的“背景噪音”活动,例如JITing。 Therefore the first time you run your method, .NET JIT's it first. 因此,第一次运行方法时,.NET JIT首先运行它。 The time it takes to do this is added to the time of the execution. 执行此操作所需的时间将添加到执行时。 Equally, other factors will also cause the execution time to vary. 同样,其他因素也会导致执行时间发生变化。

What you should really be looking for absolute accuracy is Performance Profiling ! 您应该真正寻求绝对准确性的是性能分析

Take a look at the following: 看看以下内容:

RedGate ANTS Performance Profiler is a commercial product, but produces very accurate results. RedGate ANTS Performance Profiler是一种商业产品,但可以产生非常准确的结果。 - Boost the performance of your applications with .NET profiling - 使用.NET分析提高应用程序的性能

Here is a StackOverflow article on profiling: - What Are Some Good .NET Profilers? 这是关于分析的StackOverflow文章: - 什么是一些好的.NET Profilers?

I have also written an article on Performance Profiling using Stopwatch that you may want to look at - Performance profiling in .NET 我还写了一篇关于使用秒表的性能分析的文章,你可能想看一下 - 在.NET中进行性能分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值