zoukankan      html  css  js  c++  java
  • 使用vs自带的性能诊断工具

    visual studio是个强大的集成开发环境,内置了程序性能诊断工具。下面通过两段代码进行介绍。

    static void Main( string[] args)
            {
                Test1();
                Test2();
                Console.ReadKey();
            }
            protected static void Test1()
            {
                Stopwatch sp = new Stopwatch();
                sp.Start();
                string str = "" ;
                for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        str += "string append1= " ;
                        str += i.ToString() + " ";
                        str += "string append2= " ;
                        str += j.ToString() + " ";
                    }
                }
                sp.Stop();
                Console.WriteLine("Test1 Time={0}" , sp.Elapsed.ToString());
            }
            protected static void Test2()
            {
                Stopwatch sp = new Stopwatch();
                sp.Start();
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        str.Append( "string append1= " );
                        str.Append(i.ToString());
                        str.Append( "string append2=" );
                        str.Append(j.ToString());
                    }
                }
                sp.Stop();
                Console.WriteLine("Test2 Time={0}" , sp.Elapsed.ToString());
            }
    View Code


    先运行一下查看运行结果如下:

    两个函数实现的功能都一样,实现方式不一样,效率却完全不一样,下面通过vs自带的性能分析工具进行分析,可以分析出程序对cpu使用率和内存使用情况等,
    本次以cpu测试为例。
    注:本次测试用的是vs2013,在vs2010里为启动性能向导。




     

    从以上分析结果可以得出对函数内部具体代码的的cpu使用情况,由此在实际开发中,可以针对某个代码单独拿出进行分析,以找出消耗cpu的地方,
    加以改进从而提高程序的效率。
    性能诊断工具还有不少,如微软的CLR Profiler,还有WinDbg等,后续的博客会对这两个工具作介绍。
  • 相关阅读:
    【爬坑】在 IDEA 中运行 Hadoop 程序 报 winutils.exe 不存在错误解决方案
    【爬坑】Vim 文档加密 & 解密
    Maven 安装配置
    2014/11/23 条件查询
    2014/11/21
    2014/11/20 SQL简单命令
    2014/11/19 SQL Server基础
    7、数组
    6、循环、跳转、异常语句,string类、math、datetime
    5、循环语句、穷举
  • 原文地址:https://www.cnblogs.com/kungge/p/4962339.html
Copyright © 2011-2022 走看看