Skip to content

Instantly share code, notes, and snippets.

@maxfridbe
Created November 28, 2012 16:53
Show Gist options
  • Select an option

  • Save maxfridbe/4162481 to your computer and use it in GitHub Desktop.

Select an option

Save maxfridbe/4162481 to your computer and use it in GitHub Desktop.
Timing function
public class DisposableStopwatch: IDisposable {
private readonly Stopwatch sw;
private readonly Action<TimeSpan> f;
public DisposableStopwatch(Action<TimeSpan> f) {
this.f = f;
sw = Stopwatch.StartNew();
}
public void Dispose() {
sw.Stop();
f(sw.Elapsed);
}
}
//Usage:
using (new DisposableStopwatch(t => Console.WriteLine("{0} elapsed", t)) {
// do stuff that I want to measure
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment