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

相关文章: