Dennis van der Stelt

The most votes generally drown out the best votes

Community

News

  • Meet me at PDC08

Email Notifications

I read...

I Use...

Tags

Recent Posts

Archives

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 2008\Code 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("\n\nProcessing time : {0}", elapsedTime));]]>

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

Comments

Waldek Mastykarz said:

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.

# June 13, 2008 9:23 AM

Dennis van der Stelt said:

You're right, I'll have to change it... Thanks!

# June 13, 2008 9:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)