Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 216
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

MethodStopwatch - timemeasuring class for methods


Autor: codeteq
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 1651
  
Kick it on dotnet-kicks.de  

Beschreibung:

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


Abgelegt unter: methodstopwatch, method, stopwatch, measuring, zeit, time, methode, messen, log, stream.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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

    }
}

Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

codeteq schrieb am:  19.11.2011 13:57:37

.Nt Framework 4 required


schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.