问题描述
可能重复:
如何测量运行函数多久?
我有一个 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.
推荐答案
<$c$c>Stopwatch$c$c>是专为这个目的,也是衡量时执行.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;
Do没有使用DateTime是否来测量时间执行的.NET。
Do not use DateTimes to measure time execution in .NET.
更新:
正如在评论部分@ series0ne如果你想要一些$ C $的执行真正的precise测量c您必须使用内置在操作系统中的性能计数器。该<$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.
这篇关于计算的方法的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!