c pound

I reject your reality and substitute my own!
posts - 46, comments - 37, trackbacks - 40

My Links

News

Archives

Image Galleries

Blog Communities

Blog is a stupid word

Lunch Hour

Resources

Ghetto performance measurements

This is not, by any means, the most accurate way to measure the execution time of a block of code. That said, it’s very simple and it works great when a rough idea is close enough. I think I originally got this code off CodeProejct. If I can find the article again, I’ll credit the original author here.

public class DebugTimer : IDisposable
{
        private DateTime start;
        private string label;
        private static int level = 0;

        /// <summary>
        /// Start a new DebugTimer which will run until disposed
        /// </summary>
        /// <param name="Label">A name for the running timer</param>
        /// <returns>A running DebugTimer object</returns>
        /// <remarks>This is not thread-safe</remarks>
        /// <example>
        /// public void SomeMethod ()
        /// {
        ///     using (DebugTimer.Start("My Code Block"))
        ///     {
        ///         // Code to time
        ///     }
        /// }
       
/// </example>
        public static DebugTimer Start(string Label)
        {
                return new DebugTimer(Label);
        }

        private DebugTimer(string Label)
        {
                level++;
                this.label = Label;
                this.start = DateTime.Now;
                string message = new string(' ', level) + this.label + ": Starting";
                System.Diagnostics.
Debug.WriteLine(message);
        }

        public void Dispose()
        {
                TimeSpan elapsedTime = DateTime.Now.Subtract(this.start);
                string message = new string(' ', level) + this.label + ": " + elapsedTime.ToString();
                System.Diagnostics.
Debug.WriteLine(message);
                level--;
        }
}

Print | posted on Thursday, October 13, 2005 6:58 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: