-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathBenchmarker1D.cs
More file actions
225 lines (191 loc) · 5.45 KB
/
Benchmarker1D.cs
File metadata and controls
225 lines (191 loc) · 5.45 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Waher.Runtime.Profiling
{
/// <summary>
/// Class that keeps track of benchmarking results by name and timing.
/// </summary>
public class Benchmarker1D : IBenchmarker
{
private readonly SortedDictionary<string, long> tests = new SortedDictionary<string, long>();
private readonly object syncObject = new object();
private readonly Stopwatch watch;
/// <summary>
/// Class that keeps track of benchmarking results and timing.
/// </summary>
public Benchmarker1D()
{
this.watch = new Stopwatch();
this.watch.Start();
}
/// <summary>
/// Registered tests.
/// </summary>
public string[] Tests
{
get
{
lock (this.syncObject)
{
return this.GetTestsLocked();
}
}
}
private string[] GetTestsLocked()
{
string[] Result = new string[this.tests.Count];
this.tests.Keys.CopyTo(Result, 0);
return Result;
}
/// <summary>
/// Benchmarking results, in ticks. Test names of the first index, complexities in
/// the second.
/// </summary>
public double?[] Ticks => this.GetScaledTicks(1);
/// <summary>
/// Benchmarking results, in minutes, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[] Minutes => this.GetScaledTicks(1.0 / (Stopwatch.Frequency * 60.0));
/// <summary>
/// Benchmarking results, in seconds, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[] Seconds => this.GetScaledTicks(1.0 / Stopwatch.Frequency);
/// <summary>
/// Benchmarking results, in milliseconds, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[] Milliseconds => this.GetScaledTicks(1000.0 / Stopwatch.Frequency);
/// <summary>
/// Benchmarking results, in microseconds, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[] Microseconds => this.GetScaledTicks(1000000.0 / Stopwatch.Frequency);
private double?[] GetScaledTicks(double Scale)
{
lock (this.syncObject)
{
return this.GetScaledTicksLocked(Scale);
}
}
private double?[] GetScaledTicksLocked(double Scale)
{
double?[] Result = new double?[this.tests.Count];
int i;
i = 0;
foreach (string Name in this.tests.Keys)
{
if (this.tests.TryGetValue(Name, out long Ticks))
Result[i] = Ticks * Scale;
else
Result[i] = null;
i++;
}
return Result;
}
/// <summary>
/// Gets benchmarking result in ticks, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptTicks() => this.GetResultScript(1);
/// <summary>
/// Gets benchmarking result in seconds, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptSeconds() => this.GetResultScript(1.0 / Stopwatch.Frequency);
/// <summary>
/// Gets benchmarking result in milliseconds, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptMilliseconds() => this.GetResultScript(1000.0 / Stopwatch.Frequency);
/// <summary>
/// Gets benchmarking result in microseconds, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptMicroseconds() => this.GetResultScript(1000000.0 / Stopwatch.Frequency);
private string GetResultScript(double Scale)
{
StringBuilder sb = new StringBuilder();
string[] Names;
double?[] Values;
double? d;
int i, c;
lock (this.syncObject)
{
Names = this.GetTestsLocked();
Values = this.GetScaledTicksLocked(Scale);
}
c = Names.Length;
sb.Append('[');
for (i = 0; i < c; i++)
{
if (i > 0)
sb.Append(",\r\n ");
sb.Append("[\"");
sb.Append(Names[i].Replace("\"", "\\\""));
sb.Append("\",");
d = Values[i];
if (d.HasValue)
sb.Append(d.Value.ToString().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, "."));
else
sb.Append("null");
sb.Append(']');
}
sb.Append(']');
return sb.ToString();
}
/// <summary>
/// Starts a benchmark.
/// </summary>
/// <param name="Name">Name of benchmark.</param>
/// <returns>Benchmarking object. Dispose this object when benchmarking is
/// completed.</returns>
public Benchmarking Start(string Name)
{
GC.GetTotalMemory(true);
lock (this.syncObject)
{
if (!this.tests.ContainsKey(Name))
this.tests[Name] = 0;
}
return new Benchmarking(this, Name, 0, 0, this.watch.ElapsedTicks);
}
/// <summary>
/// Stops a benchmark.
/// </summary>
/// <param name="Name">Name of benchmark.</param>
/// <param name="N">Complexity of benchmark (N).</param>
/// <param name="M">Complexity of benchmark (M).</param>
/// <param name="StartTicks">Elapsed ticks at the start of benchmark.</param>
public void Stop(string Name, long N, long M, long StartTicks)
{
long ElapsedTicks = this.watch.ElapsedTicks - StartTicks;
lock (this.syncObject)
{
this.tests[Name] = ElapsedTicks;
}
}
/// <summary>
/// Removes a benchmark.
/// </summary>
/// <param name="Name">Name of benchmark.</param>
/// <returns>If the benchmark was found, and removed.</returns>
public bool Remove(string Name)
{
lock (this.syncObject)
{
return this.tests.Remove(Name);
}
}
/// <summary>
/// Disposes of the object.
/// </summary>
public void Dispose()
{
this.watch.Stop();
}
}
}