Created
February 2, 2015 19:07
-
-
Save cliss/f03f7268a1c9006daf88 to your computer and use it in GitHub Desktop.
C# Debug Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DebugTimer : IDisposable | |
{ | |
private readonly System.Diagnostics.Stopwatch _watch; | |
private readonly string _blockName; | |
/// <summary> | |
/// Creates a timer. | |
/// </summary> | |
/// <param name="blockName">Name of the block that's being timed</param> | |
public DebugTimer(string blockName) | |
{ | |
_blockName = blockName; | |
_watch = System.Diagnostics.Stopwatch.StartNew(); | |
} | |
public void Dispose() | |
{ | |
_watch.Stop(); | |
GC.SuppressFinalize(this); | |
System.Diagnostics.Debug.WriteLine(_blockName + ": " + _watch.Elapsed.TotalSeconds + " seconds."); | |
} | |
~DebugTimer() | |
{ | |
throw new InvalidOperationException("Must Dispose() of all instances of " + this.GetType().FullName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Foo() | |
{ | |
using (new DebugTimer("Foo()")) | |
{ | |
// Do work | |
} | |
} | |
// In the Visual Studio Output window: | |
// Foo(): 1.2345 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're able to target C# 5/.Net 4.5 you can save yourself from potential refactoring issues falling out of sync by using the
CallerMemberName
Attribute.If you might need to call it in different places in the same method, you can extend the constructor to take an extra parameter to capture that info.