(C#)记录程序运行时间

使用Stopwatch类

在使用前需要添加引用:using System.Diagnostics;

static void Main(string[] args)
{
string s =null;
//创建一个计时器,用来记录程序的运行时间
Stopwatch sw = new Stopwatch();
sw.Start();//开始计时
for (int i = 0; i < 100; i++)
{
s += i;//s为字符串类型,i为整型。+号起连接作用,自动将i变为字符串类型加进来
}
sw.Stop();//结束计时
Console.WriteLine(sw.Elapsed);
Console.ReadKey();
}

程序运行结果:
(C#)记录程序运行时间