.NET : 使用代码性能分析工具

上一篇,我演示了如何使用CLR Profiler对.NET应用程序进行性能分析。下面再谈谈在Visual Studio中自带的工具

示范代码

using System;
using System.Text;
/// <summary>
/// 这个例子程序用来演示如何进行.NET程序的性能监视和调优
/// 作者:陈希章
/// </summary>
public class ProfilerSample1
{
    static void Main(string[] args)
    {
        int start = Environment.TickCount;
        for (int i = 0; i < 1000; i++)
        {
            string s = "";
            for (int j = 0; j < 100; j++)
            {
                s += "Outer index = ";
                s += i;
                s += " Inner index = ";
                s += j;
                s += " ";
            }
        }

        #endregion


        Console.WriteLine("Program ran for {0} seconds",
            0.001 * (Environment.TickCount - start));
    }
}

image

运行代码分析工具(需要Visual Studio Team suite for developer版本)

"分析"==〉“启动性能向导”

image

image

image

image

现在可以启动性能监视了,很快就能看到下面的一个报表

image

通过这个摘要报告,我们可以看出来,该程序中占绝大部分的操作都是string.concat方法,就是字符串连接。一共有500000次。

从时间上也可以看到它们占用了绝大部分时间

image

image

image

将程序修改为使用stringbuidler

image

image

原文地址:https://www.cnblogs.com/chenxizhang/p/1623079.html