Stopwatch提供了几个方法用以控制Stopwatch对象。Start方法开始一个计时操作,Stop方法停止计时。此时如果第二次使用Start方法,将继续计时,最终的计时结果为两次计时的累加。为避免这种情况,在第二次计时前用Reset方法将对象归零。这三个方法都不需要参数。代码是: using System; using System.Diagnostics;
namespace StopWatchClass { class Program { static void Main(string[] args) { Stopwatch timer = new Stopwatch(); long total = 0;
timer.Start(); for (int i = 1; i <= 10000000; i++) { total += i; }
timer.Stop(); } } } 读取Stopwatch结果: <!--[if !supportLists]--><!--[endif]--> 在结束计时后下一步就是读取计时结果了。 上一页 [1] [2] [3] [4] [5] 下一页 |