C# 計時程序運行時間


 

第一種   System.DateTime

public static void SubTest()
        {
            DateTime beforeDT = System.DateTime.Now;
            int[] a = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
            //Shuffle(a) is the function you want to test.
            Shuffle(a);
            DateTime afterDT = System.DateTime.Now;
            TimeSpan ts = afterDT.Subtract(beforeDT);
            Console.WriteLine("DateTime costed for Shuffle function is: {0}ms",ts.TotalMilliseconds);
        }

 

第二種用Stopwatch類(System.Diagnostics)

 /// <summary>
        /// 測試for循環優化
        /// </summary>
        /// <returns></returns>
        [HttpGet("test")]
        public ActionResult<ApiResponse> test()
        {
            var result = new ApiResponse();
           Stopwatch sw = new Stopwatch();
           sw.Start();
           //耗時程序

            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            Console.WriteLine("DateTime costed for Shuffle function is: {0}ms", ts.TotalMilliseconds);
            return result;

        }

 轉自:https://www.cnblogs.com/I-am-Betty/p/10489787.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM