计算的方法的执行时间 [英] Calculate the execution time of a method

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

问题描述

可能重复:
  如何测量运行函数多久?

我有一个 I / O 时间服用方法,将数据从一个位置到另一个位置。什么是计算的执行时间的最好,最真实的方式? 定时器秒表?任何其他的解决办法?我想最确切的一个,最短的尽可能多的。

I have an I/O time taking method which copies data from a location to another. What's the best and most real way of calculating the execution time? Thread? Timer? Stopwatch? Any other solution? I want the most exact one, and briefest as much as possible.

推荐答案

<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx"><$c$c>Stopwatch是专为这个目的,也是衡量时执行.NET中的最佳途径之一。

Stopwatch is designed for this purpose and is one of the best way to measure time execution in .NET.

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

<一个href="http://stackoverflow.com/questions/28637/is-datetime-now-the-best-way-to-measure-a-functions-performance">Do没有使用DateTime是否来测量时间执行的.NET。

Do not use DateTimes to measure time execution in .NET.

更新:

正如在评论部分@ series0ne如果你想要一些$ C $的执行真正的precise测量c您必须使用内置在操作系统中的性能计数器。该<一href="http://stackoverflow.com/questions/1409762/creating-a-perfmon-counter-to-record-an-average-per-call-c/1409833#1409833"><$c$c>following答案 包含了一个很好的概述。

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. The following answer contains a nice overview.

这篇关于计算的方法的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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