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="">
  <codesnippet format="1.0.0">

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

      <code language="csharp">
        <![CDATA[Stopwatch sw = new Stopwatch();


      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));]]>