【代码保留】时间器(用于计算时间间隔的类)
动机:
主要是想要做一个时间间隔的类。晚上随便写的,可以用来测试效率的一些性能。暂不支持调试(调试的时候时间会暂停,但是时间间隔类的内部实现机制是采用取头尾的方式进行的,因此暂时不支持调试~以后看看会不会用到,用到再写吧~)
代码:
主要是想要做一个时间间隔的类。晚上随便写的,可以用来测试效率的一些性能。暂不支持调试(调试的时候时间会暂停,但是时间间隔类的内部实现机制是采用取头尾的方式进行的,因此暂时不支持调试~以后看看会不会用到,用到再写吧~)
代码:
1
/// <summary>
2
/// 时间器类,可以单独使用,也可以与时间器工厂+时间器管理器共同使用
3
/// (单独使用,将失去全局比较的优良特性,时间器管理器提供了对时间器的片段式、全局式的管理能力,适合更恶劣的环境。)
4
/// </summary>
5
public class Timer
6
{
7
private TimeSpan timeSpan;
8
public TimeSpan TimeSpan
9
{
10
get
11
{
12
if(this.state == TimerState.Runing)
13
this.Stop();
14
return this.timeSpan;
15
}
16
}
17![](/Images/OutliningIndicators/InBlock.gif)
18
public enum TimerState
19
{
20
UnStarted,
21
Runing,
22
Stopped
23![](/Images/OutliningIndicators/InBlock.gif)
24
}
25
private TimerState state = TimerState.UnStarted;
26
public TimerState State
27
{
28
get
29
{
30
return this.state;
31
}
32
}
33![](/Images/OutliningIndicators/InBlock.gif)
34
private DateTime startTime;
35
private DateTime endTime;
36![](/Images/OutliningIndicators/InBlock.gif)
37
public void Start()
38
{
39
startTime = DateTime.Now;
40
this.state = TimerState.Runing;
41
}
42![](/Images/OutliningIndicators/InBlock.gif)
43
public void Stop()
44
{
45
endTime = DateTime.Now;
46
timeSpan = endTime - startTime;
47
this.state = TimerState.Stopped;
48
}
49
}
50![](/Images/OutliningIndicators/None.gif)
51
/// <summary>
52
/// 所有由时间器工厂创建的时间器将被添加到全局时间管理器中。可以利用key进行索引。
53
/// </summary>
54
public class TimerFactory
55
{
56
public static Timer GetNewTimer(object key)
57
{
58
TimerManager.Add(key, new Timer());
59
return TimerManager.Get(key);
60
}
61![](/Images/OutliningIndicators/InBlock.gif)
62
public static Timer GetNewTimerGolbal(ref string key)
63
{
64
key = key + "(" + DateTime.Now.ToString() + ")";
65
return TimerFactory.GetNewTimer(key);
66
}
67
}
68![](/Images/OutliningIndicators/None.gif)
69
/// <summary>
70
/// 时间器管理器
71
/// </summary>
72
public class TimerManager
73
{
74
private static Dictionary<object, Timer> Timers = new Dictionary<object, Timer>();
75
public static Timer Get(object key)
76
{
77
Timer result = Timers[key];
78
if (result == null)
79
result = new Timer();
80
return result;
81
}
82![](/Images/OutliningIndicators/InBlock.gif)
83
/// <summary>
84
/// key最好不一样,为了方便管理,也为了便于时间器的找回。否则同KEY的时间器将被重新启动,旧有的时间信息将不被保留。
85
/// 通常在一个段内将可以区分段内时间器的时间信息。
86
/// </summary>
87
/// <param name="key"></param>
88
/// <param name="value"></param>
89
public static void Add(object key, Timer value)
90
{
91
if (!Timers.ContainsKey(key))
92
Timers.Add(key, value);
93
}
94
}
95
//////////////////////////////////////////////////////////////////////////////////
96
//Example1(normal):
97
//////////////////////////////////////////////////////////////////////////////////
98
//string FilterTimerKey = "FilterTimer";
99
//TimerFactory.GetNewTimer(FilterTimerKey).Start();
100![](/Images/OutliningIndicators/None.gif)
101
//IList<int> result = CollectionBase<int>.Filter((IList<int>)stardardRegion, (IList<int>)orginalRegion);
102![](/Images/OutliningIndicators/None.gif)
103
//string ConsoleTimerKey = "ConsoleTimer";
104
//TimerFactory.GetNewTimer(ConsoleTimerKey).Start();
105![](/Images/OutliningIndicators/None.gif)
106
//Console.WriteLine("Filter result:(select Count(*) from this) = {0}", result.Count);
107
//foreach (int item in result)
108
//{
109
// Console.WriteLine("{0}",item);
110
//}
111![](/Images/OutliningIndicators/None.gif)
112
//Console.WriteLine("{0}:{1}", FilterTimerKey, TimerManager.Get(FilterTimerKey).TimeSpan.TotalSeconds);
113
//Console.WriteLine("{0}:{1}", ConsoleTimerKey, TimerManager.Get(ConsoleTimerKey).TimeSpan.TotalSeconds);
114![](/Images/OutliningIndicators/None.gif)
115
////FilterTimer:4.5625
116
////ConsoleTimer:0.234375
117![](/Images/OutliningIndicators/InBlock.gif)
118
//////////////////////////////////////////////////////////////////////////////////
119
//Example2(golbal):
120
//////////////////////////////////////////////////////////////////////////////////
121
//string FilterTimerKey = "FilterTimer";
122
//TimerFactory.GetNewTimerGolbal(ref FilterTimerKey).Start();
123![](/Images/OutliningIndicators/None.gif)
124
//IList<int> result = CollectionBase<int>.Filter((IList<int>)stardardRegion, (IList<int>)orginalRegion);
125![](/Images/OutliningIndicators/None.gif)
126
//string ConsoleTimerKey = "ConsoleTimer";
127
//TimerFactory.GetNewTimerGolbal(ref ConsoleTimerKey).Start();
128![](/Images/OutliningIndicators/None.gif)
129
//Console.WriteLine("Filter result:(select Count(*) from this) = {0}", result.Count);
130
////foreach (int item in result)
131
////{
132
//// Console.WriteLine("{0}",item);
133
////}
134![](/Images/OutliningIndicators/None.gif)
135
//Console.WriteLine("{0}:{1}", FilterTimerKey, TimerManager.Get(FilterTimerKey).TimeSpan.TotalSeconds);
136
//Console.WriteLine("{0}:{1}", ConsoleTimerKey, TimerManager.Get(ConsoleTimerKey).TimeSpan.TotalSeconds);
137
////FilterTimer:4.5625
138
////ConsoleTimer:0.234375
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
54
![](/Images/OutliningIndicators/None.gif)
55
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
68
![](/Images/OutliningIndicators/None.gif)
69
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
72
![](/Images/OutliningIndicators/None.gif)
73
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
94
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
95
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
96
![](/Images/OutliningIndicators/None.gif)
97
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
98
![](/Images/OutliningIndicators/None.gif)
99
![](/Images/OutliningIndicators/None.gif)
100
![](/Images/OutliningIndicators/None.gif)
101
![](/Images/OutliningIndicators/None.gif)
102
![](/Images/OutliningIndicators/None.gif)
103
![](/Images/OutliningIndicators/None.gif)
104
![](/Images/OutliningIndicators/None.gif)
105
![](/Images/OutliningIndicators/None.gif)
106
![](/Images/OutliningIndicators/None.gif)
107
![](/Images/OutliningIndicators/None.gif)
108
![](/Images/OutliningIndicators/None.gif)
109
![](/Images/OutliningIndicators/None.gif)
110
![](/Images/OutliningIndicators/None.gif)
111
![](/Images/OutliningIndicators/None.gif)
112
![](/Images/OutliningIndicators/None.gif)
113
![](/Images/OutliningIndicators/None.gif)
114
![](/Images/OutliningIndicators/None.gif)
115
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
119
![](/Images/OutliningIndicators/None.gif)
120
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
121
![](/Images/OutliningIndicators/None.gif)
122
![](/Images/OutliningIndicators/None.gif)
123
![](/Images/OutliningIndicators/None.gif)
124
![](/Images/OutliningIndicators/None.gif)
125
![](/Images/OutliningIndicators/None.gif)
126
![](/Images/OutliningIndicators/None.gif)
127
![](/Images/OutliningIndicators/None.gif)
128
![](/Images/OutliningIndicators/None.gif)
129
![](/Images/OutliningIndicators/None.gif)
130
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
134
![](/Images/OutliningIndicators/None.gif)
135
![](/Images/OutliningIndicators/None.gif)
136
![](/Images/OutliningIndicators/None.gif)
137
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
138
![](/Images/OutliningIndicators/InBlock.gif)
posted on 2007-11-01 01:06 volnet(可以叫我大V) 阅读(637) 评论(2) 编辑 收藏 举报