Last active
August 29, 2015 14:25
-
-
Save restlessmedia/06acfbcdcc68d58174de to your computer and use it in GitHub Desktop.
TimeBlock - using block for profiling
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 TimeBlock : IDisposable | |
{ | |
public TimeBlock(Action<int> handler = null, bool start = true) | |
{ | |
_handler = handler; | |
_watch = new Stopwatch(); | |
if (start) | |
Start(); | |
} | |
public void Start() | |
{ | |
_watch.Start(); | |
} | |
public void Stop() | |
{ | |
_watch.Stop(); | |
if (_handler != null) | |
_handler(Elapsed); | |
} | |
public void Dispose() | |
{ | |
Stop(); | |
} | |
public int Elapsed | |
{ | |
get | |
{ | |
return _watch.Elapsed.Milliseconds; | |
} | |
} | |
public static TimeBlock Debug(string label) | |
{ | |
return new TimeBlock(x => string.Concat(x, "ms ", label)); | |
} | |
private readonly Action<int> _handler; | |
private readonly Stopwatch _watch; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using(TimeBlock.Debug("a routine"))
{
..
}