赞
踩
目录
Stopwatch:提供一组方法和属性,可以准确的测量运行时间。使用的时候需要引用命名空间:System.Diagnostics。
- //创建Stopwatch实例
- Stopwatch sw = new Stopwatch();
- //开始计时
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- Console.WriteLine(i);
- }
- //停止计时
- sw.Stop();
- Console.WriteLine("用时:" + sw.ElapsedMilliseconds + "");
- //重置 停止时间间隔测量,并将运行时间重置为0
- sw.Reset();
- Console.WriteLine("用时:" + sw.ElapsedMilliseconds + "");
- //重启 停止时间间隔测量,并将运行时间重置为0,然后重新开始测量运行时间
- sw.Restart();
- for (int i = 0; i < 1000; i++)
- {
- Console.WriteLine(i);
- }
- sw.Stop();
- //获取当前实例测量得出的总运行时间(以毫秒为单位)
- Console.WriteLine("用时:" + sw.ElapsedMilliseconds + "");
- //获取当前实例测量得出的总运行时间
- Console.WriteLine("用时:" + sw.Elapsed);
- //获取当前实例测量得出的总运行时间(用计时器刻度表示)。
- Console.WriteLine(sw.ElapsedTicks);
- Console.Read();
- //使用StartNew,相当于已经实例化并且启动计时
- Stopwatch sw1 = Stopwatch.StartNew();
- for (int i = 0; i < 100; i++)
- {
- Console.WriteLine(i);
- }
- sw1.Stop();
- //获取当前实例测量得出的总运行时间(以毫秒为单位)
- Console.WriteLine("用时:" + sw1.ElapsedMilliseconds + "");
- //获取当前实例测量得出的总运行时间
- Console.WriteLine("用时:" + sw1.Elapsed);
- Console.Read();

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。