可以使用类 Stopwatch
来实现记录一段程序所需要的时间。
引用命名空间:
using System.Diagnostics;
:用于类Stopwatch
;using System.Threading;
类Thread
,为了演示效果,让程序休眠。
见下方代码示例:
using System;
using System.Diagnostics;
using System.Threading;
namespace Demos
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("程序开始......");
Stopwatch stopwatch = Stopwatch.StartNew(); // 开始程序片段计时
Thread.Sleep(5300); // 线程休眠5.3秒
Console.WriteLine("从开始计时到这里共用时{0}毫秒!", stopwatch.ElapsedMilliseconds); // 计时结束,单位毫秒
Console.ReadKey();
}
}
}