计算代码段运行的时间长度,用Stopwatch类
1Stopwatch sw = new Stopwatch();
2sw.Start();
3Thread.Sleep(3000);
4sw.Stop();
5Console.WriteLine(sw.ElapsedMilliseconds());
2sw.Start();
3Thread.Sleep(3000);
4sw.Stop();
5Console.WriteLine(sw.ElapsedMilliseconds());
每次获取时间都要写这么几行,为此封装一个计算时间的类
Code
调用的时候:
1using (RunningTimer timer = new RunningTimer("test"))
2{
3 Thread.Sleep(3000);
4}
2{
3 Thread.Sleep(3000);
4}