Stopwatch snippet

A little snippet that I use from time to time when I need information on how long something takes. Type ‘sw’ (without the quotes) and tab-tab.

Installation is easy, just create a file called “sw.snippet” in your %Documents%Visual Studio 2008Code Snippets folder and paste the following in the file. You can use it immediately without restarting Visual Studio.

<?xml version="1.0" encoding="utf-8" ?>
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <codesnippet format="1.0.0">

      <shortcut>sw</shortcut>
      <description>Code snippet for adding complete Stopwatch usage and display of elapsed time.</description>
      <author>Dennis van der Stelt</author>
      <snippettypes>
        <snippettype>Expansion</snippettype>
      </snippettypes>

    <snippet>
      <references>
        <reference>
          <assembly>System.Diagnostics</assembly>
        </reference>
      </references>
      <code language="csharp">
        <![CDATA[Stopwatch sw = new Stopwatch();
      sw.Start();

      sw.Stop();

      TimeSpan ts = sw.Elapsed;
      string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                          ts.Hours, ts.Minutes, ts.Seconds,
                                          ts.Milliseconds / 10);
      Console.WriteLine(String.Format("nnProcessing time : {0}", elapsedTime));]]>
      </code>
    </snippet>
  </codesnippet>
</codesnippets>