1: using System.Collections.Generic;
2:
3: namespace Ia.Cl.Model
4: {
5: ////////////////////////////////////////////////////////////////////////////
6:
7: /// <summary publish="true">
8: /// Stopwatch model
9: /// </summary>
10: ///
11: /// <remarks>
12: /// Copyright © 2020-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
13: ///
14: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
15: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16: ///
17: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19: ///
20: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
21: ///
22: /// Copyright notice: This notice may not be removed or altered from any source distribution.
23: /// </remarks>
24: public class Stopwatch
25: {
26: private long lastElapsedMilliseconds;
27: private readonly System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
28: private readonly List<string> list = new List<string>();
29:
30: ////////////////////////////////////////////////////////////////////////////
31:
32: /// <summary>
33: ///
34: /// </summary>
35: public Stopwatch()
36: {
37: stopwatch.Start();
38:
39: lastElapsedMilliseconds = 0;
40:
41: list.Add("Start");
42: }
43:
44: ////////////////////////////////////////////////////////////////////////////
45:
46: /// <summary>
47: ///
48: /// </summary>
49: public void Start()
50: {
51: stopwatch.Start();
52:
53: lastElapsedMilliseconds = 0;
54:
55: list.Add("Start");
56: }
57:
58: ////////////////////////////////////////////////////////////////////////////
59:
60: /// <summary>
61: ///
62: /// </summary>
63: public void RecordElapsedMilliseconds()
64: {
65: long l = stopwatch.ElapsedMilliseconds - lastElapsedMilliseconds;
66:
67: list.Add(l.ToString());
68:
69: lastElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
70: }
71:
72: ////////////////////////////////////////////////////////////////////////////
73:
74: /// <summary>
75: ///
76: /// </summary>
77: public void RecordElapsedMilliseconds(string label)
78: {
79: long l = stopwatch.ElapsedMilliseconds - lastElapsedMilliseconds;
80:
81: list.Add(label + ":" + l.ToString().PadLeft(5, ' '));
82:
83: lastElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
84: }
85:
86: ////////////////////////////////////////////////////////////////////////////
87:
88: /// <summary>
89: ///
90: /// </summary>
91: public long ElapsedMilliseconds
92: {
93: get
94: {
95: return stopwatch.ElapsedMilliseconds;
96: }
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: ///
103: /// </summary>
104: public void Stop()
105: {
106: stopwatch.Stop();
107:
108: list.Add("Stop: ElapsedMilliseconds:" + stopwatch.ElapsedMilliseconds);
109: }
110:
111: ////////////////////////////////////////////////////////////////////////////
112:
113: /// <summary>
114: ///
115: /// </summary>
116: public void Reset()
117: {
118: stopwatch.Reset();
119:
120: lastElapsedMilliseconds = 0;
121: list.Clear();
122: }
123:
124: ////////////////////////////////////////////////////////////////////////////
125:
126: /// <summary>
127: ///
128: /// </summary>
129: public string List()
130: {
131: return string.Join(", ", list);
132: }
133:
134: ////////////////////////////////////////////////////////////////////////////
135: ////////////////////////////////////////////////////////////////////////////
136: }
137:
138: ////////////////////////////////////////////////////////////////////////////
139: ////////////////////////////////////////////////////////////////////////////
140: }