0
点赞
收藏
分享

微信扫一扫

C# 统计程序执行时间

C#统计程序执行时间,代码如下:

class Program
    {

        static void Main(string[] args)
        {
            string s = "chen";
            string ss = "chen";
            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 100000000; i++)
            {
                if (s == ss)
                {
                    //Console.WriteLine("字符串相同");
                }
                else
                {
                    //Console.WriteLine("字符串不相同");
                }

            }
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("sw总共花费{0}ms.", ts2.TotalMilliseconds);

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

            for (int i = 0; i < 100000000; i++)
            {
                if (s.Equals(ss))
                {
                    //Console.WriteLine("字符串相同");
                }
                else
                {
                    //Console.WriteLine("字符串不相同");
                }

            }
            sw_Eq.Stop();
            TimeSpan tssw_Eq = sw_Eq.Elapsed;
            Console.WriteLine("sw_Eq总共花费{0}ms.", tssw_Eq.TotalMilliseconds);
           
            Console.ReadKey();
        }

     
    }

   当然,咱们也可以自行写一个方法,很简单的,就是程序执行前,统计下当前时间,执行完成后,统计下当前时间,两个当前时间相减即可

   代码如下:

class Program
    {

        static void Main(string[] args)
        {
            string s = "chen";
            string ss = "chen";
            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 100000000; i++)
            {
                if (s == ss)
                {
                    //Console.WriteLine("字符串相同");
                }
                else
                {
                    //Console.WriteLine("字符串不相同");
                }

            }
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("sw总共花费{0}ms.", ts2.TotalMilliseconds);

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

            for (int i = 0; i < 100000000; i++)
            {
                if (s.Equals(ss))
                {
                    //Console.WriteLine("字符串相同");
                }
                else
                {
                    //Console.WriteLine("字符串不相同");
                }

            }
            sw_Eq.Stop();
            TimeSpan tssw_Eq = sw_Eq.Elapsed;
            Console.WriteLine("sw_Eq总共花费{0}ms.", tssw_Eq.TotalMilliseconds);


            DateTime beforDT = System.DateTime.Now;

             for (int i = 0; i < 100000000; i++)
            {
                if (s.Equals(ss))
                {
                    //Console.WriteLine("字符串相同");
                }
                else
                {
                    //Console.WriteLine("字符串不相同");
                }

            }

            DateTime afterDT = System.DateTime.Now;
            TimeSpan ts = afterDT.Subtract(beforDT);
            Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds);  

            Console.ReadKey();
        }
    }


举报

相关推荐

0 条评论