Feedback

C# - MethodStopwatch - timemeasuring class for methods

Veröffentlicht von am 18.11.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
GFU-Schulungen  [Anzeige]

XML und .NET Überblick

Um auf dem neuesten Wissensstand zu sein, sollten Sie unser aktuelles ASP .NET Komplett Seminar belegen.
Nach dem Seminar kennen Sie die wichtigsten Strömungen in der Software-Technologie

ASP.NET Core - Der Einstieg

In nur 3 Tagen lernen Sie, wie man mit ASP.NET Core und den Technologien MVC, Entity Framework, WebAPI und Razor professionelle Web-Anwendungs-Architekturen aufbaut. Diese Schulung ist der perfekte Einstieg, um datengetriebene und präsentations-orientierte Applikationen auf Basis der ASP.NET Core Plattform zu erstellen.

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 19.11.2011:
.Nt Framework 4 required
 

Logge dich ein, um hier zu kommentieren!