Feedback

C# - MethodStopwatch - timemeasuring class for methods

Veröffentlicht von am 11/18/2011
(1 Bewertungen)
MethodStopwatch is a easy to use static class for measuring the time of a method (i.e. by simple calling "Start" at the begin of a method and "Stop" or "StopAndPublish" at the end.
by codeteq, 2011
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;

namespace TimeMeasuring
{
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++
    // MethodStopwatch is a easy to use static class for measuring the time 
    // of a method (i.e. by simple calling "Start" at the begin of a method and "Stop" or "StopAndPublish" at the end 
    // by codeteq, 2011
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++

    public static class MethodStopwatch
    {

        #region private classes

        private class _MethodStopwatch : Stopwatch
        {

            #region consts, members, properties

            public System.Reflection.MethodBase Method
            {
                get
                {
                    try
                    {
                        StackFrame sf = new StackFrame(4);
                        return sf.GetMethod();
                    }
                    catch { return null; }
                }
            }

            #endregion

            #region .ctor

            public _MethodStopwatch(bool autostart = true)
            {
                if (autostart)
                    this.Start();
            }

            #endregion

            #region methods

            public string StopAndPublish(Stream stream, string suffix = null)
            {
                this.Stop();
                return Publish(stream, suffix);
            }

            public string Publish(Stream stream, string suffix)
            {
                string output = String.Format("Method: \"{0}\" lasted: {1}ms {2}", (this.Method != null) ? this.Method.Name : "Couldn not resolve method", this.ElapsedMilliseconds.ToString(), suffix);
                if (stream == null)
                {
                    if (Debugger.IsAttached)
                        Debug.WriteLine(output);
                }
                else if(stream.CanWrite)
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                        writer.WriteLine(output);
                }
                return output;
            }

            #endregion

        }

        #endregion

        #region consts, members, properties

        private static Stack<_MethodStopwatch> stopwatches;

        #endregion

        #region methods

        public static void Start()
        {
            (stopwatches = (stopwatches = new Stack<_MethodStopwatch>())).Push(new _MethodStopwatch());
        }

        public static void Stop()
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().Stop();
        }

        public static void Publish(string suffix = null)
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().Publish(null, suffix);
        }

        public static void StopAndPublish(string suffix = null)
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().StopAndPublish(null, suffix);
        }

        public static void PublishToStream(Stream stream, string suffix = null)
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().Publish(stream, suffix);
        }

        public static void StopAndPublishToStream(Stream stream, string suffix = null)
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().StopAndPublish(stream, suffix);
        }

        public static void Restart()
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().Restart();
        }

        public static void Reset()
        {
            if (stopwatches != null && stopwatches.Count > 0)
                stopwatches.Pop().Reset();
        }

        #endregion

    }
}

1 Kommentare zum Snippet

codeteq schrieb am 11/19/2011:
.Nt Framework 4 required
 

Logge dich ein, um hier zu kommentieren!