计算的方法的执行时间执行时间、方法

2023-09-02 20:45:03 作者:百合花对猫是致命的毒性

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

我有一个 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.

推荐答案

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;

Do没有使用DateTime是否来测量时间执行的.NET。

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

更新:

正如在评论部分@ series0ne如果你想要一些$ C $的执行真正的precise测量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.

 
精彩推荐
图片推荐