计算代码段运行的时间长度,用Stopwatch类
1
Stopwatch sw = new Stopwatch();
2
sw.Start();
3
Thread.Sleep(3000);
4
sw.Stop();
5
Console.WriteLine(sw.ElapsedMilliseconds());

2

3

4

5

每次获取时间都要写这么几行,为此封装一个计算时间的类

调用的时候:
1
using (RunningTimer timer = new RunningTimer("test"))
2
{
3
Thread.Sleep(3000);
4
}

2

3

4
