C#中Stopwatch的使用及说明

  private static void TimeOperations()

  {

  long nanosecPerTick = (1000L * 1000L * 1000L) / Stopwatch.Frequency;

  const long numIterations = 10000;

  // 定义操作标题名称

  String[] operationNames = {"操作: Int32.Parse("0")",

  "操作: Int32.TryParse("0")",

  "操作: Int32.Parse("a")",

  "操作: Int32.TryParse("a")"};

  Console.WriteLine();

  Console.WriteLine("注:1ticks=100ns,1s=1000ms,1ms=1000us,1us=1000ns");

  // 从字符串解析整数的四种不同实现

  for (int operation = 0; operation <= 3; operation++)

  {

  // 定义操作统计的变量

  long numTicks = 0;

  long numRollovers = 0;

  long maxTicks = 0;

  long minTicks = Int64.MaxValue;

  int indexFastest = -1;

  int indexSlowest = -1;

  long milliSec = 0;

  Stopwatch time10kOperations = Stopwatch.StartNew();

  // 运行当前操作10001次。

  // 第一次执行时间将被丢弃,因为它可能会扭曲平均时间。

  for (int i = 0; i <= numIterations; i++)

  {

  long ticksThisTime = 0;

  int inputNum;

  Stopwatch timePerParse;

  switch (operation)

  {

  case 0:

  // 使用try-catch语句分析有效整数

  // 启动新的秒表计时器

  timePerParse = Stopwatch.StartNew();

  try

  {

  inputNum = Int32.Parse("0");

  }

  catch (FormatException)

  {

  inputNum = 0;

  }

  // 停止计时器,并保存操作所用的计时ticks

  timePerParse.Stop();

  ticksThisTime = timePerParse.ElapsedTicks;

  break;

  case 1:

  timePerParse = Stopwatch.StartNew();

  if (!Int32.TryParse("0", out inputNum))

  {

  inputNum = 0;

  }

  timePerParse.Stop();

  ticksThisTime = timePerParse.ElapsedTicks;

  break;

  case 2:

  timePerParse = Stopwatch.StartNew();

  try

  {

  inputNum = Int32.Parse("a");

  }

  catch (FormatException)

  {

  inputNum = 0;

  }

  timePerParse.Stop();

  ticksThisTime = timePerParse.ElapsedTicks;

  break;

  case 3:

  timePerParse = Stopwatch.StartNew();

  if (!Int32.TryParse("a", out inputNum))

  {

  inputNum = 0;

  }

  timePerParse.Stop();

  ticksThisTime = timePerParse.ElapsedTicks;

  break;

  default:

  break;

  }

  // 跳过第一个操作的时间,以防它导致一次性性能下降。

  if (i == 0)

  {

  time10kOperations.Reset();

  time10kOperations.Start();

  }

  else

  {

  // 更新迭代1-10001的操作统计信息。

  if (maxTicks < ticksThisTime)

  {

  indexSlowest = i;

  maxTicks = ticksThisTime;

  }

  if (minTicks > ticksThisTime)

  {

  indexFastest = i;

  minTicks = ticksThisTime;

  }

  numTicks += ticksThisTime;

  if (numTicks < ticksThisTime)

  {

  // Keep track of rollovers.

  numRollovers++;

  }

  }

  }

  // 显示10000次迭代的统计信息

  time10kOperations.Stop();

  milliSec = time10kOperations.ElapsedMilliseconds;

  Console.WriteLine();

  Console.WriteLine("{0} 统计:", operationNames[operation]);

  Console.WriteLine("  最慢时间:  第{0}/{1}次操作,时间为{2} ticks",

  indexSlowest, numIterations, maxTicks);

  Console.WriteLine("  最快时间:  第{0}/{1}次操作,时间为{2} ticks",

  indexFastest, numIterations, minTicks);

  Console.WriteLine("  平均时间:  {0} ticks = {1} ns",

  numTicks / numIterations,

  (numTicks * nanosecPerTick) / numIterations);

  Console.WriteLine("  {0} 次操作的总时间: {1} ms",

  numIterations, milliSec);

  }

  }