用法
主要通过Stopwatch
类来实现...
在开发、调试、测试、分析中非常实用.
Stopwatch sw = new Stopwatch();sw.Start();// 某些耗时的计算或任务...sw.Stop();var runtime = sw.ElapsedMilliseconds;
实例
新建一个叫做StopwatchDemo控制台应用程序
using System;using System.Diagnostics;namespace StopwatchDemo{ class Program { static void Main(string[] args) { Console.WriteLine("开始计算..."); Stopwatch sw = new Stopwatch(); sw.Start(); int sum = 0; for (int i = 0, n = 100 * 10000; i < n; i++) { sum += i; } sw.Stop(); Console.WriteLine("执行百万次累加计算共耗时{0}毫秒", sw.ElapsedMilliseconds); Console.WriteLine("累加结果为:{0}", sum); Console.ReadKey(); } }}