3using System.Diagnostics;
4using System.Globalization;
15 private readonly SortedDictionary<string, bool> tests =
new SortedDictionary<string, bool>();
16 private readonly SortedDictionary<long, bool> complexitiesN =
new SortedDictionary<long, bool>();
17 private readonly SortedDictionary<long, bool> complexitiesM =
new SortedDictionary<long, bool>();
18 private readonly Dictionary<string, Dictionary<long, Dictionary<long, long>>> ticks =
new Dictionary<string, Dictionary<long, Dictionary<long, long>>>();
19 private readonly
object syncObject =
new object();
20 private readonly Stopwatch watch;
28 this.watch =
new Stopwatch();
39 lock (this.syncObject)
41 return this.GetTestsLocked();
46 private string[] GetTestsLocked()
48 string[] Result =
new string[this.tests.Count];
49 this.tests.Keys.CopyTo(Result, 0);
60 lock (this.syncObject)
62 return this.GetComplexitiesNLocked();
67 private long[] GetComplexitiesNLocked()
69 long[] Result =
new long[this.complexitiesN.Count];
70 this.complexitiesN.Keys.CopyTo(Result, 0);
81 lock (this.syncObject)
83 return this.GetComplexitiesMLocked();
88 private long[] GetComplexitiesMLocked()
90 long[] Result =
new long[this.complexitiesM.Count];
91 this.complexitiesM.Keys.CopyTo(Result, 0);
99 public double?[,,]
Ticks => this.GetScaledTicks(1);
105 public double?[,,]
Minutes => this.GetScaledTicks(1.0 / (Stopwatch.Frequency * 60.0));
111 public double?[,,]
Seconds => this.GetScaledTicks(1.0 / Stopwatch.Frequency);
117 public double?[,,]
Milliseconds => this.GetScaledTicks(1000.0 / Stopwatch.Frequency);
123 public double?[,,]
Microseconds => this.GetScaledTicks(1000000.0 / Stopwatch.Frequency);
125 private double?[,,] GetScaledTicks(
double Scale)
127 lock (this.syncObject)
129 return this.GetScaledTicksLocked(Scale);
133 private double?[,,] GetScaledTicksLocked(
double Scale)
135 lock (this.syncObject)
137 double?[,,] Result =
new double?[this.tests.Count, this.complexitiesN.Count,
138 this.complexitiesM.Count];
142 foreach (
string Name
in this.tests.Keys)
145 foreach (
long ComplexityN
in this.complexitiesN.Keys)
148 foreach (
long ComplexityM
in this.complexitiesM.Keys)
150 if (this.ticks.TryGetValue(Name, out Dictionary<
long, Dictionary<long, long>>
ComplexitiesN) &&
154 Result[i, j, k] =
Ticks * Scale;
157 Result[i, j, k] =
null;
196 private string GetResultScript(
double Scale)
198 StringBuilder sb =
new StringBuilder();
204 int i, j, k, c, N, M;
206 lock (this.syncObject)
208 Names = this.GetTestsLocked();
211 Values = this.GetScaledTicksLocked(Scale);
219 for (i = 0; i < c; i++)
224 sb.Append(
"\r\n\t\"");
225 sb.Append(Names[i].Replace(
"\"",
"\\\""));
226 sb.Append(
"\":\r\n\t\t[[\"N\\\\M\"");
231 sb.Append(Complexity.ToString());
236 for (j = 0; j < N; j++)
238 sb.Append(
",\r\n\t\t [");
241 for (k = 0; k < M; k++)
247 sb.Append(d.Value.ToString(CultureInfo.InvariantCulture));
263 return sb.ToString();
276 GC.GetTotalMemory(
true);
278 lock (this.syncObject)
280 if (!this.tests.ContainsKey(Name))
281 this.tests[Name] =
true;
283 if (!this.complexitiesN.ContainsKey(N))
284 this.complexitiesN[N] =
true;
286 if (!this.complexitiesM.ContainsKey(M))
287 this.complexitiesM[M] =
true;
290 return new Benchmarking(
this, Name, N, M, this.watch.ElapsedTicks);
300 public void Stop(
string Name,
long N,
long M,
long StartTicks)
302 long ElapsedTicks = this.watch.ElapsedTicks - StartTicks;
304 lock (this.syncObject)
306 if (!this.ticks.TryGetValue(Name, out Dictionary<
long, Dictionary<long, long>>
ComplexitiesN))
308 ComplexitiesN =
new Dictionary<long, Dictionary<long, long>>();
329 lock (this.syncObject)
331 this.ticks.Remove(Name);
332 return this.tests.Remove(Name);
343 return this.
Remove(N,
null);
354 bool Removed =
false;
356 lock (this.syncObject)
358 foreach (Dictionary<
long, Dictionary<long, long>>
ComplexitiesN in this.ticks.Values)
370 if (N.HasValue &&
this.complexitiesN.Remove(N.Value))
373 if (M.HasValue &&
this.complexitiesM.Remove(M.Value))
Class that keeps track of benchmarking results by name, two complexities N and M, and timing.
double?[,,] Microseconds
Benchmarking results, in microseconds, Test names of the first index, complexities N in the second an...
string GetResultScriptMilliseconds()
Gets benchmarking result in milliseconds, as script.
void Dispose()
Disposes of the object.
long[] ComplexitiesM
Registered complexities (M).
double?[,,] Milliseconds
Benchmarking results, in milliseconds, Test names of the first index, complexities N in the second an...
string GetResultScriptSeconds()
Gets benchmarking result in seconds, as script.
string[] Tests
Registered tests.
Benchmarking Start(string Name, long N, long M)
Starts a benchmark.
string GetResultScriptMicroseconds()
Gets benchmarking result in microseconds, as script.
double?[,,] Minutes
Benchmarking results, in minutes, Test names of the first index, complexities N in the second and com...
bool Remove(string Name)
Removes a benchmark.
long[] ComplexitiesN
Registered complexities (N).
double?[,,] Ticks
Benchmarking results, in ticks. Test names of the first index, complexities N in the second and compl...
string GetResultScriptTicks()
Gets benchmarking result in ticks, as script.
void Stop(string Name, long N, long M, long StartTicks)
Stops a benchmark.
bool Remove(long? N)
Removes a complexity.
Benchmarker3D()
Class that keeps track of benchmarking results by name, two complexities N and M, and timing.
double?[,,] Seconds
Benchmarking results, in seconds, Test names of the first index, complexities N in the second and com...
bool Remove(long? N, long? M)
Removes a complexity.
Class that keeps track of benchmarking results and timing.
Class that keeps track of benchmarking results and timing.