Skip to content

Instantly share code, notes, and snippets.

@flagbug
Last active August 29, 2015 13:56
Show Gist options
  • Save flagbug/9292785 to your computer and use it in GitHub Desktop.
Save flagbug/9292785 to your computer and use it in GitHub Desktop.
Measure
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();
}
}
}
}
using(MeasureHelper.Measure())
{
MeasureMe();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment