You are on page 1of 11

vb4arab.

com

Microsoft

.net

Microsoft .net Framework

02

*




MSDN

Microsoft .Net

: ) (
.
3 .I .II .III .




.


/ /
-
28 1428
2007/07/14

2007

System.Diagnostics
Stopwatch

.1
.2

.3
.4

.5
.6

Stopwatch

Stopwatch
-
- )(Methods
- )(Properties


- Visual Basic .NET
.4 .1. : For For Each
.4 .2. : String
StringBuilder
- Visual C#
.4 .1. : for foreach
.4 .2. : String
StringBuilder
.NET!!!

Performance

.net 2.0

03
I

System.Diagnostics

.net 2.0

Stopwatch

 .1


.2

 Stopwatch
.NET Microsoft Framework .NET 2.0

.
Microsoft Framework .Net
Stopwatch System.Diagnostics

) 7-10 ( 3 !

.2   .

1
2
3
4

:
)(.Reset
)( Start .
)( Stop .
Elapsed ElapsedMilliseconds .ElapsedTicks

.2 .
)(Methods
.

)
)(Reset
) 0 ( Nothing null- . (ElapsedXXX
.
)(Start

.ElapsedXXX
IsRunning .false
)(Stop
:1.2 .Stopwatch
ElapsedXXX : Elapsed ElapsedTicks
ElapsedMilliseconds

Performance



) (Debugging


.

.

04

System.Diagnostics

Performance

.net 2.0

05

Stopwatch

Stop() Start()
.TimeSpan
710) 100) Ticks
( Tick 1 = (

.
.

( Properties) '()*+
..2

System.TimeSpan

Elapsed

System.Int64

ElapsedTicks

System.Int64

ElapsedMilliseconds

System.Boolean

IsRunning

Stopwatch :2.2

2 4 54 67 89 =<;) .3


.
System.String
.

) @?)4  ..3


System.Diagnostics Stopwatch
Imports System.Diagnostics 'VB .NET * +,using System.Diagnostics; //C# * +,-

:C<
 D)EF GHI 2 K<


 LH ..3
Private Function Concat(ByRef Target As String, ByVal newChar As
Char, ByVal nb As Integer) As Boolean 'VB .NET * +,If nb <= 0 Then
Return False
Else
Dim i As Integer
For i = 1 To nb
Target &= newChar
Next
Return True
End If
End Function
//C# * +,private bool Concat (ref string Target,char newChar,int nb)
{
if (nb <= 0)
return false;
else
{
for (int i = 0; i < nb; i++)
Target += newChar;
return true;
}
}

. :1.3

Performance

.net 2.0

06

System.Diagnostics
Stopwatch

50000 NC 67  ?


;)Stopwatch 
) H<? ..3
. =G;G? R<4 KD =
'VB .NET * +,Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Stopwatch GOPD EF GHIJ LMJ
Dim mySWatch As New Stopwatch()
Dim myStrTest As String
' LIS GTUVW YZ
mySWatch.Start()
' [\TP]^ _` LIb YcdJ \D fgh-
Concat(myStrTest, "A"c, 50000)
' LIS GTUVW kT`l^
mySWatch.Stop()
' mno- p db 50000 GrLs GTUVW \TP]oD dnoI-`_ lD Lvw
Label1.Text = mySWatch.ElapsedMilliseconds.ToString & _
" GTJLy *UTF"
End Sub
//*+,- C#
private void button1_Click(object sender, System.EventArgs e)
{
Stopwatch mySWatch = new Stopwatch();
string
myStrTest;
//LIS GTUVW YZ
mySWatch.Start();
// fgh-\ D YcdJ LIb _`\[ TP]^
Concat(ref myStrTest, 'A', 50000);
//LIS GTUVW kT`l^
mySWatch.Stop();
// mno- p db 50000 GrLsz \P]oI-`_ lD Lvw
Label1.Text = mySWatch.ElapsedMilliseconds.ToString() + "
GTJLy"
}

*UTF

. 50000 :2.3

T<
 GU4@ VHI .4

for) For Each For
StringBuilder String ( C# foreach
.Visual C# Visual Basic .NET
:
) ( . JIT MSIL
.
)
.(
System.Text StringBuilder -

System.Diagnostics

.net 2.0

Stopwatch

Performance

Visual Basic .NET K4T


..4

07

For Each For <G


67  ? I `)4 :) @U
.1..4
Private Sub btnSWatchTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSWatchTest.Click
Dim mySWatch As New Stopwatch()
Dim myArrayList As New Collections.ArrayList()
'GV{L`
Dim i As Integer
'GV{LhD| F
For i = 1 To 5000
myArrayList.Add(i)
Next
'For Each Gcd}oD GhUS LV~oLZ fgh-\ TP]oD D`_ lD LIb
mySWatch.Start()
For Each i In myArrayList
lstItems.Items.Add(i)
Next
mySWatch.Stop()
'7-10 LIS G`
txtForEach.Text = mySWatch.ElapsedTicks.ToString()
'For Gcd}oD GhUS LV~oLZ fgh-\ TP]oD D`_ lD LIb
lstItems.Items.Clear()
mySWatch.Reset()
mySWatch.Start()
For i = 0 To myArrayList.Count - 1
lstItems.Items.Add(myArrayList(i))
Next
mySWatch.Stop()
'7-10 LIS G`
txtFor.Text = mySWatch.ElapsedTicks.ToString()
End Sub

. :1.4

. For Each For :1.4

.net 2.0

System.Diagnostics
Stopwatch

Performance

StringBuilder String <G+


 I `)4 :K`)U
) U
.2..4

08

Private Sub btnSWatchTest_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnSWatchTest.Click
Dim strTest As String
Dim mySWatch As New Stopwatch()
'System.String GcL~D GOPD LV~o
mySWatch.Start()
Call ConcatStr(strTest, "a"c, 50000)
mySWatch.Stop()
'7-10 LIS G`
txtStr.Text = mySWatch.ElapsedTicks.ToString()
'System.Text.StringBuilder GOPD LV~o
mySWatch.Reset()
mySWatch.Start()
Call ConcatStrBuilder(strTest, "a"c, 50000)
mySWatch.Stop()
'7-10 LIS G`
txtStrBuilder.Text = mySWatch.ElapsedTicks.ToString()
End Sub
Private Function ConcatStr(ByRef Target As String, ByVal newChar As Char,
ByVal nb As Integer) As Boolean
If nb <= 0 Then
Return False
Else
Dim i As Integer
For i = 1 To nb
Target &= newChar
Next
Return True
End If
End Function
Private Function ConcatStrBuilder(ByRef Target As String, ByVal newChar As
Char, ByVal nb As Integer) As Boolean
If nb <= 0 Then
Return False
Else
Dim strBuilder As New System.Text.StringBuilder(nb nb)
strBuilder.Append(newChar, nb)
Target = strBuilder.ToString()
Return True
End If
End Function

. 50000 :2.4


:2.4


StringBuilder

System.Diagnostics

.net 2.0

Stopwatch

Performance

Visual C# K4T
..4

09

foreach for <G


67  ? I `)4 :) @U
.1..4

private void btnSWatchTest_Click(object sender, System.EventArgs e)


{
Stopwatch mySWatch = new Stopwatch();
Collections.ArrayList myArrayList = new Collections.ArrayList();
//GV{LhD| F
int i=1;
for (; i <= 5000; i++)
myArrayList.Add(i);
//foreach Gcd}oD GhUS LV~oLZ fgh-\ TP]oD D`_ lD LIb
mySWatch.Start();
foreach (int i in myArrayList)
lstItems.Items.Add(i);
mySWatch.Stop();
'7-10 LIS G`
txtForEach.Text = mySWatch.ElapsedTicks.ToString();
//for Gcd}oD GhUS LV~oLZ fgh-\ TP]oD D`_ lD LIb
lstItems.Items.Clear();
mySWatch.Reset();
mySWatch.Start();
for (i = 0; i <= myArrayList.Count - 1;i++)
lstItems.Items.Add(myArrayList(i));
mySWatch.Stop();
'7-10 LIS G`
txtFor.Text = mySWatch.ElapsedTicks.ToString();
}

. C# .1..4 :3.4

System.Diagnostics

.net 2.0

Stopwatch

Performance

StringBuilder String <G+


 I `)4 :K`)U
) U
.2..4

10

private void btnSWatchTest_Click(object sender, System.EventArgs e)


{
string strTest ;
Stopwatch mySWatch =new Stopwatch();
//System.String GcL~D GOPD LV~o
mySWatch.Start();
ConcatStr(strTest, 'a', 50000);
mySWatch.Stop();
//7-10 LIS G`
txtStr.Text = mySWatch.ElapsedTicks.ToString();
mySWatch.Reset();
//System.Text.StringBuilder GOPD LV~o
mySWatch.Start();
ConcatStrBuilder(strTest, 'a', 50000);
mySWatch.Stop();
//7-10 LIS G`
txtStrBuilder.Text = mySWatch.ElapsedTicks.ToString();
}
private bool ConcatStr(ref string Target ,char newChar , int nb )
{
if (nb<=0)
return false;
else
{
for (int i = 0; i < nb; i++)
Target += newChar;
return true;
}
}
private bool ConcatStrBuilder(ref string Target ,char newChar , int nb )
{
if (nb <=0 )
return false;
else
{
System.Text.StringBuilder strBuilder = new
System.Text.StringBuilder(nb,nb);
strBuilder.Append(newChar,nb);
Target = strBuilder.ToString();
return true;
}
}

. C# .2..4 :3.4

System.Diagnostics

.net 2.0

Stopwatch

)4 .5
 c 4T
2 )e2
27C<
T
 ` 8!!!

*+, 'vb6.0Private Declare Function GetTickCount Lib "kernel32" () As Long

Performance

API Visual Basic Visual


C++.
GetTickCount
100) Ticks = (Tick 1 Session.

11

*/
*+, C++YD Grd~F GD windows.h :header
*/
"#include "windows.h

:
1 .long
2 .long
3 )
(.

)i .6
Stopwatch
.NET Framework 2.0
.


.


.

2007

You might also like