主要是想要做一个时间间隔的类。晚上随便写的,可以用来测试效率的一些性能。暂不支持调试(调试的时候时间会暂停,但是时间间隔类的内部实现机制是采用取头尾的方式进行的,因此暂时不支持调试~以后看看会不会用到,用到再写吧~)
代码:
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
18 public enum TimerState
19 {
20 UnStarted,
21 Runing,
22 Stopped
23
24 }
25 private TimerState state = TimerState.UnStarted;
26 public TimerState State
27 {
28 get
29 {
30 return this.state;
31 }
32 }
33
34 private DateTime startTime;
35 private DateTime endTime;
36
37 public void Start()
38 {
39 startTime = DateTime.Now;
40 this.state = TimerState.Runing;
41 }
42
43 public void Stop()
44 {
45 endTime = DateTime.Now;
46 timeSpan = endTime - startTime;
47 this.state = TimerState.Stopped;
48 }
49 }
50
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
62 public static Timer GetNewTimerGolbal(ref string key)
63 {
64 key = key + "(" + DateTime.Now.ToString() + ")";
65 return TimerFactory.GetNewTimer(key);
66 }
67 }
68
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
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
101 //IList<int> result = CollectionBase<int>.Filter((IList<int>)stardardRegion, (IList<int>)orginalRegion);
102
103 //string ConsoleTimerKey = "ConsoleTimer";
104 //TimerFactory.GetNewTimer(ConsoleTimerKey).Start();
105
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
112 //Console.WriteLine("{0}:{1}", FilterTimerKey, TimerManager.Get(FilterTimerKey).TimeSpan.TotalSeconds);
113 //Console.WriteLine("{0}:{1}", ConsoleTimerKey, TimerManager.Get(ConsoleTimerKey).TimeSpan.TotalSeconds);
114
115 ////FilterTimer:4.5625
116 ////ConsoleTimer:0.234375
117
118 //////////////////////////////////////////////////////////////////////////////////
119 //Example2(golbal):
120 //////////////////////////////////////////////////////////////////////////////////
121 //string FilterTimerKey = "FilterTimer";
122 //TimerFactory.GetNewTimerGolbal(ref FilterTimerKey).Start();
123
124 //IList<int> result = CollectionBase<int>.Filter((IList<int>)stardardRegion, (IList<int>)orginalRegion);
125
126 //string ConsoleTimerKey = "ConsoleTimer";
127 //TimerFactory.GetNewTimerGolbal(ref ConsoleTimerKey).Start();
128
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
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
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
18 public enum TimerState
19 {
20 UnStarted,
21 Runing,
22 Stopped
23
24 }
25 private TimerState state = TimerState.UnStarted;
26 public TimerState State
27 {
28 get
29 {
30 return this.state;
31 }
32 }
33
34 private DateTime startTime;
35 private DateTime endTime;
36
37 public void Start()
38 {
39 startTime = DateTime.Now;
40 this.state = TimerState.Runing;
41 }
42
43 public void Stop()
44 {
45 endTime = DateTime.Now;
46 timeSpan = endTime - startTime;
47 this.state = TimerState.Stopped;
48 }
49 }
50
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
62 public static Timer GetNewTimerGolbal(ref string key)
63 {
64 key = key + "(" + DateTime.Now.ToString() + ")";
65 return TimerFactory.GetNewTimer(key);
66 }
67 }
68
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
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
101 //IList<int> result = CollectionBase<int>.Filter((IList<int>)stardardRegion, (IList<int>)orginalRegion);
102
103 //string ConsoleTimerKey = "ConsoleTimer";
104 //TimerFactory.GetNewTimer(ConsoleTimerKey).Start();
105
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
112 //Console.WriteLine("{0}:{1}", FilterTimerKey, TimerManager.Get(FilterTimerKey).TimeSpan.TotalSeconds);
113 //Console.WriteLine("{0}:{1}", ConsoleTimerKey, TimerManager.Get(ConsoleTimerKey).TimeSpan.TotalSeconds);
114
115 ////FilterTimer:4.5625
116 ////ConsoleTimer:0.234375
117
118 //////////////////////////////////////////////////////////////////////////////////
119 //Example2(golbal):
120 //////////////////////////////////////////////////////////////////////////////////
121 //string FilterTimerKey = "FilterTimer";
122 //TimerFactory.GetNewTimerGolbal(ref FilterTimerKey).Start();
123
124 //IList<int> result = CollectionBase<int>.Filter((IList<int>)stardardRegion, (IList<int>)orginalRegion);
125
126 //string ConsoleTimerKey = "ConsoleTimer";
127 //TimerFactory.GetNewTimerGolbal(ref ConsoleTimerKey).Start();
128
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
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