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">

    <Header>

      <Title>Add Stopwatch code</Title>

      <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>

    </Header>

    <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>

You may also like...

2 Responses

  1. Personally I would change the snippet type to SurroundWith, so you are able to select the code you want to measure and then surround it with the snippet using CTRL+K, S.

  2. Dennis van der Stelt says:

    You’re right, I’ll have to change it… Thanks!

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.