System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // 开始监视代码运行时间
//需要监测的代码
stopwatch.Stop(); // 停止监视
TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
string hours = timespan.TotalHours.ToString("#0.00000000 "); // 总小时
string minutes = timespan.TotalMinutes.ToString("#0.00000000 "); // 总分钟
string seconds = timespan.TotalSeconds.ToString("#0.00000000 "); // 总秒数
string milliseconds = timespan.TotalMilliseconds.ToString("#0.00000000 "); // 总毫秒数
原文:http://www.cnblogs.com/wu-peng/p/6818090.html