我需要找到一个瓶颈,并需要尽可能准确地测量时间。

下面的代码片段是衡量性能的最佳方法吗?

DateTime startTime = DateTime.Now;

// Some execution process

DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);

当前回答

我在程序中使用的方式是使用StopWatch类,如下所示。

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


// Critical lines of code

long elapsedMs = sw.Elapsed.TotalMilliseconds;

其他回答

这些都是衡量时间的好方法,但这只是发现瓶颈的一种非常间接的方法。

在线程中找到瓶颈的最直接方法是让它运行,当它正在做任何让您等待的事情时,使用暂停或中断键停止它。这样做几次。如果你的瓶颈占用了X%的时间,那么X%就是你在每次快照中捕捉到它的概率。

这里有一个更完整的解释,它是如何以及为什么工作的

秒表也一样,它好多了。

关于性能度量,你还应该检查你的“// Some Execution Process”是否是一个非常短的过程。

还要记住,“// Some Execution Process”的第一次运行可能比后续运行慢得多。

我通常通过在循环中运行1000次或100万次来测试一个方法,我得到的数据比运行一次要准确得多。

这是正确的方法:

using System;
using System.Diagnostics;

class Program
{
    public static void Main()
    {
        Stopwatch stopWatch = Stopwatch.StartNew();

            // some other code

        stopWatch.Stop();

        // this not correct to get full timer resolution
        Console.WriteLine("{0} ms", stopWatch.ElapsedMilliseconds);

        // Correct way to get accurate high precision timing
        Console.WriteLine("{0} ms", stopWatch.Elapsed.TotalMilliseconds);
    }
}

要了解更多信息,请使用秒表而不是DataTime来获得准确的性能计数器。

如果你想要一些快速和肮脏的东西,我建议使用秒表代替更大程度的精度。

Stopwatch sw = new Stopwatch();
sw.Start();
// Do Work
sw.Stop();

Console.WriteLine("Elapsed time: {0}", sw.Elapsed.TotalMilliseconds);

或者,如果你需要一些更复杂的东西,你可能应该考虑使用第三方分析器,如ANTS。

将基准测试代码放入实用程序类/方法中是很有用的。StopWatch类不需要在错误时被丢弃或停止。最简单的为动作计时的代码是

public partial class With
{
    public static long Benchmark(Action action)
    {
        var stopwatch = Stopwatch.StartNew();
        action();
        stopwatch.Stop();
        return stopwatch.ElapsedMilliseconds;
    }
}

示例调用代码

public void Execute(Action action)
{
    var time = With.Benchmark(action);
    log.DebugFormat(“Did action in {0} ms.”, time);
}

下面是扩展方法的版本

public static class Extensions
{
    public static long Benchmark(this Action action)
    {
        return With.Benchmark(action);
    }
}

以及示例调用代码

public void Execute(Action action)
{
    var time = action.Benchmark()
    log.DebugFormat(“Did action in {0} ms.”, time);
}