As of .NET 2.0 there is a new class called StopWatch in System.Diagnostics. It is very easy to use. I just found it, while playing around with my Md5 optimization "project".

For all of you that can't upgrade to Framework 2.0 yet - here is the implementation I used to time my md5 algos. It is loosely based on the high performance timer Daniel Strigl posted on codeproject. I enhanced it a little bit in order to be able to use it running Mono on Linux. In that case it defaults to DateTime.Tick accuracy.

C#:
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4.  
  5. namespace Md5Optimized
  6. {
  7.     public class PerformanceTimer
  8.     {
  9.         public static readonly bool IsHighPerformance;
  10.  
  11.         [DllImport("Kernel32.dll")]
  12.         private static extern bool QueryPerformanceCounter(
  13.                 out long lpPerformanceCount);
  14.  
  15.         [DllImport("Kernel32.dll")]
  16.         private static extern bool QueryPerformanceFrequency(
  17.                 out long lpFrequency);
  18.  
  19.         private long m_startTime;
  20.         private long m_stopTime;
  21.         private static long m_freq;
  22.  
  23.         static PerformanceTimer()
  24.         {
  25.             try
  26.             {
  27.                 IsHighPerformance =
  28.                    QueryPerformanceFrequency(out m_freq);
  29.             }
  30.             catch (Exception)
  31.             {
  32.                 IsHighPerformance = false;
  33.             }
  34.  
  35.         }
  36.  
  37.         public PerformanceTimer()
  38.         {
  39.             m_startTime = 0;
  40.             m_stopTime = 0;
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Start the timer
  45.         /// </summary>
  46.         public void Start()
  47.         {
  48.             // let the waiting threads do their work
  49.             Thread.Sleep(0);
  50.  
  51.             if (IsHighPerformance)
  52.             {
  53.                 QueryPerformanceCounter(out m_startTime);
  54.             }
  55.             else
  56.             {
  57.                 m_startTime = DateTime.Now.Ticks;
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Stop the timer
  63.         /// </summary>
  64.         public void Stop()
  65.         {
  66.             if (IsHighPerformance)
  67.             {
  68.                 QueryPerformanceCounter(out m_stopTime);
  69.             }
  70.             else
  71.             {
  72.                 m_stopTime = DateTime.Now.Ticks;
  73.             }
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Returns the duration of the timer
  78.         /// (in fraction of seconds)
  79.         /// </summary>         
  80.         public double DurationSeconds
  81.         {
  82.             get
  83.             {
  84.                 if (IsHighPerformance)
  85.                 {
  86.                     return (double)(m_stopTime - m_startTime) /
  87.                         (double)m_freq;
  88.                 }
  89.                 else
  90.                 {
  91.                     TimeSpan span =
  92.                         (new DateTime(m_stopTime)) -
  93.                         (new DateTime(m_startTime));
  94.                     return span.TotalSeconds;
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }

Please keep in mind that this class is a quick hack. It works... ;) but only if used correctly.

Download PerformanceTimer.cs

Post to Twitter Tweet this