C#中检测代码执行时间

 使用System.Diagnostics.Stopwatch,

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);
    }
}

https://learn.microsoft.com/zh-cn/dotnet/api/system.diagnostics.stopwatch?view=net-7.0

posted @ 2023-01-29 19:22  东羽白帝  阅读(92)  评论(0编辑  收藏  举报