使用 Stopwatch
// 创建并启动计时器
var stopwatch = Stopwatch.StartNew();
// 要测量的代码
YourCodeToMeasure();
// 停止计时
stopwatch.Stop();
Console.WriteLine($"执行时间:{stopwatch.Elapsed.ToString(@"m\分s\.fff\秒")}");
使用 Stopwatch
// 创建并启动计时器
var stopwatch = Stopwatch.StartNew();
// 要测量的代码
YourCodeToMeasure();
// 停止计时
stopwatch.Stop();
Console.WriteLine($"执行时间:{stopwatch.Elapsed.ToString(@"m\分s\.fff\秒")}");