Tuesday, 16 October 2012

C# Simple Code Timer



Tried of writing datetimes all over the place to measure performace, do it the noob way!

public class watch : IDisposable
    {
        private Stopwatch iSWatch;
        private string name;

        public watch(string name)
        {
#if DEBUG
            this.name = name;
            iSWatch = new Stopwatch();
            iSWatch.Start();
#endif
        }

        public void Dispose()
        {
#if DEBUG
            iSWatch.Stop();
            Debug.WriteLine(name + ": " + iSWatch.Elapsed);
#endif
        }
    }







Usage:


using (var timer = new watch("Some Name"))
{
        //Code to time here
}

No comments:

Post a Comment