Created
November 28, 2012 16:53
-
-
Save maxfridbe/4162481 to your computer and use it in GitHub Desktop.
Timing function
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 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