Skip to content

Instantly share code, notes, and snippets.

@restlessmedia
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restlessmedia/06acfbcdcc68d58174de to your computer and use it in GitHub Desktop.
Save restlessmedia/06acfbcdcc68d58174de to your computer and use it in GitHub Desktop.
TimeBlock - using block for profiling
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;
}
@restlessmedia
Copy link
Author

using(TimeBlock.Debug("a routine"))
{
..
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment