执行时间测量
.net frameWork 提供了一组相应的诊断程序,System.Diagnostics.Stopwatch提供一组关于运行时间测量的属性和方法。
Stopwatch stopwatch = new Stopwatch();
stopwatch .Start();
//some Code you need run
stopwatch .Stop();
string time =stopwatch .ElapsedMilliseconds.ToString();
stopwatch .Start();
//some Code you need run
stopwatch .Stop();
string time =stopwatch .ElapsedMilliseconds.ToString();
很简单就能看出中间代码执行消耗的时间了。
有兴趣可以用宏玩:
#if DEBUG
Stopwatch stopwatch = new Stopwatch();
stopwatch .Start();
#endif
//some Code you need run
#if DEBUG
stopwatch .Stop();
string time =stopwatch .ElapsedMilliseconds.ToString();
#endif
Stopwatch stopwatch = new Stopwatch();
stopwatch .Start();
#endif
//some Code you need run
#if DEBUG
stopwatch .Stop();
string time =stopwatch .ElapsedMilliseconds.ToString();
#endif