VS2010自帶的性能分析工具分析.NET程序的性能

這篇博文給你們分享的是,如何使用VS自帶的性能分析工具來分析咱們編寫的.NET程序,一邊找出程序性能的瓶頸,改善代碼的質量。在實際開發中,性能真的很重要,每每決定一個產品的生死~良好的用戶體驗的基礎之一也是程序要有好的性能~函數

下面以一個你們熟悉比較極端的例子,來講明編寫代碼時考慮性能的重要性。這裏DebugLZQ用的是10.0版本的VS。工具

示例程序代碼以下:性能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VS2010性能測試
{
    class Program
    {
        static void Main(string[] args)
        {
            int start = Environment.TickCount;
            for (int i = 0; i < 1000; i++)
            {
                string s = "";
                for (int j = 0; j <200; j++)
                {
                    s += "Outer index = ";
                    s += i;
                    s += " Inner index = ";
                    s += j;
                    s += " ";
                }
            }
            int middle = Environment.TickCount;
            Console.WriteLine("Program part1 run for {0} seconds",0.001 * (middle  - start));
            //
            for (int i = 0; i < 1000; i++)
            {
                StringBuilder s = new StringBuilder(); 
                for (int j = 0; j <200; j++)
                {
                    s.Append("Outer index = ");
                    s.Append(i);
                    s.Append("Inner index = ");
                    s.Append(j);
                    s.Append(" ");
                }
            }
            int end = Environment.TickCount;
            Console.WriteLine("Program part2 run for {0} seconds", 0.001 * (end - middle));

            //
            Console.ReadKey();
        }
    }
}測試

差距就是這麼大!ui

咱們能夠使用VS自帶的性能分析工具來分析這個程序。能夠經過「分析」--「啓動性能嚮導」來啓動性能分析spa

咱們能夠根據須要選擇不一樣的分析方法開發

面以「CPU採樣」分析爲例源碼

切換到函數視圖string

定位到咱們的源碼:產品

問題找到了~

相關文章
相關標籤/搜索