Skip to content

Instantly share code, notes, and snippets.

@bernhard-hofmann
Last active July 12, 2018 15:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernhard-hofmann/956893 to your computer and use it in GitHub Desktop.
Save bernhard-hofmann/956893 to your computer and use it in GitHub Desktop.
C# snippet to surround code with a stopwatch
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Stopwatch</Title>
<Shortcut>SW</Shortcut>
<Description>Code snippet for Stopwatch</Description>
<Author>Bernhard Hofmann</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[var tacho = System.Diagnostics.Stopwatch.StartNew();
$selected$ $end$
tacho.Stop();
System.Diagnostics.Trace.WriteLine(string.Format("Elapsed: {0}", tacho.Elapsed));
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
@andreigavrila
Copy link

Great. Thanks. You should use the static method Stopwatch.StartNew() to both allocate a new stopwatch and start it in a single line

@bernhard-hofmann
Copy link
Author

Thanks Andrei, I've made that change. 5 month turnaround - not bad eh? :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment