如何计算c#应用程序的执行时间

Kal*_*ani 4 c#

如何计算c#应用程序的执行时间.我有c#windows应用程序,我需要计算执行时间,我不知道我必须在哪里继续这个.谁能帮帮我吗?

Nik*_*wal 14

使用System.Diagnostics的秒表

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);
}
Run Code Online (Sandbox Code Playgroud)