轉貼文章:http://tfeng.org/?p=1215 逐風者
這是 .net framework 2.0 新增的類別,可以讓你知道您得程式耗費多少時間,有助於優化自己得程式!
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 | VB.net Code:
Dim sw As New System.Diagnostics.Stopwatch()
Dim num As Long = 0
sw.Reset()
sw = Stopwatch.StartNew()
'要測速的程式碼放這裡sw.Stop()
Dim ms As Long = sw.ElapsedMilliseconds
Debug.Print("花費 {0} 毫秒", ms)
Dim el As TimeSpan = sw.Elapsed
Debug.Print("花費 {0} ", el)
範例:(檢查do......loop花的時間)
Dim sw As New System.Diagnostics.Stopwatch()
Dim num As Long = 0
sw.Reset()
sw = Stopwatch.StartNew()
Do unitle x>100
x += 1
loop
sw.Stop()
Dim ms As Long = sw.ElapsedMilliseconds
Debug.Print("花費 {0} 毫秒", ms)
Dim el As TimeSpan = sw.Elapsed
Debug.Print("花費 {0} ", el) |
C# Code:
1
2
3
4
5
6
7
8
9
10
11
12 | using System.Diagnostics;
//-------------------------------------------
Stopwatch sw = new Stopwatch();
long num = 0;
sw.Reset();
sw = Stopwatch.StartNew();
//要測速的程式放這裡
sw.Stop();
TimeSpan el = sw.Elapsed;
Console.WriteLine("花費 {0} ", el);
long ms = sw.ElapsedMilliseconds;
Console.WriteLine("花費 {0} 毫秒", ms); |
補充說明: 不一定每次測到的時間都相同喔!
建議值: 超過100毫秒就有點太慢囉…. (電腦爛會Lag更長)
留言
張貼留言