-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeedTimer.cs
118 lines (103 loc) · 2.83 KB
/
SpeedTimer.cs
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
/*
* Created by SharpDevelop.
* User: Joe
* Date: 14/06/2010
* Time: 9:31 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
namespace ET2Solver
{
/// <summary>
/// Description of SpeedTimer.
/// </summary>
public class SpeedTimer_Data
{
// timer variables
public string id = "";
public bool running = false;
public int numIterations = 0;
public int duration = 1;
public int target_iterations = 0;
public double target_time = 0;
public double start_time = 0;
public double end_time = 0;
public double elapsed = 0;
public double speed = 0.0;
public SpeedTimer_Data()
{
}
}
public class SpeedTimer
{
public Dictionary<string, SpeedTimer_Data> timers = new Dictionary<string, SpeedTimer_Data>();
public SpeedTimer()
{
}
public void start(string id)
{
if ( this.timers.ContainsKey(id) )
{
this.timers[id].start_time = DateTime.Now.Ticks;
this.timers[id].running = true;
this.timers[id].target_time = this.timers[id].start_time + (this.timers[id].duration * 10000000.0);
}
else
{
SpeedTimer_Data timer = new SpeedTimer_Data();
timer.id = id;
timer.start_time = DateTime.Now.Ticks;
timer.target_time = timer.start_time + (timer.duration * 10000000.0);
timer.end_time = 0;
timer.elapsed = 0;
timer.speed = 0.0;
timer.numIterations = 0;
timer.running = true;
this.timers.Add(id, timer);
}
}
public void stop(string id)
{
if ( this.timers[id].running )
{
this.timers[id].numIterations++;
this.timers[id].end_time = DateTime.Now.Ticks;
this.timers[id].elapsed += (this.timers[id].end_time - this.timers[id].start_time);
this.timers[id].running = false;
}
}
public void iterate(string id)
{
this.timers[id].numIterations++;
}
public double timeElapsed(string id)
{
this.stop(id);
return this.timers[id].elapsed / 10000000.0;
}
public bool durationReached(string id)
{
return DateTime.Now.Ticks >= this.timers[id].target_time;
}
public bool iterationsReached(string id)
{
return this.timers[id].numIterations >= this.timers[id].target_iterations;
}
public string calcSpeed(string id)
{
this.timers[id].speed = this.timers[id].numIterations / this.timeElapsed(id);
return String.Format("{0} - {1:#,#} iterations in {2:0.000} second(s) @ {3:#,#.0} it/sec", id, this.timers[id].numIterations, this.timeElapsed(id), this.timers[id].speed);
}
public string getStats()
{
List<string> stats = new List<string>();
foreach ( string id in this.timers.Keys )
{
stats.Add(this.calcSpeed(id));
}
return String.Join("\r\n", stats.ToArray());
}
}
}