Last active
August 29, 2015 13:56
-
-
Save flagbug/9292785 to your computer and use it in GitHub Desktop.
Measure
This file contains hidden or 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 static class MeasureHelper | |
{ | |
public static IDisposable Measure([CallerMemberName] string caller = null) | |
{ | |
var stopWatch = Stopwatch.StartNew(); | |
return Disposable.Create(() => | |
{ | |
stopWatch.Stop(); | |
Console.WriteLine("Measured in {0}: {1}", caller, stopWatch.Elapsed); | |
}); | |
} | |
// Include this, if you don't have System.Reactive.Disposable | |
private class Disposable : IDisposable | |
{ | |
private Action disposable; | |
private Disposable(Action disposable) | |
{ | |
this.disposable = disposable; | |
} | |
public static Disposable Create(Action disposable) | |
{ | |
return new Disposable(disposable); | |
} | |
public void Dispose() | |
{ | |
var dispose = Interlocked.Exchange(ref disposable, null); | |
if (dispose != null) | |
{ | |
dispose(); | |
} | |
} | |
} | |
} |
This file contains hidden or 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
using(MeasureHelper.Measure()) | |
{ | |
MeasureMe(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment