vb.net 也加入了范型,令人振奋,但是今天写了一个测试的例子,其结果,却让我失望不已
这叫什么啊~~~~
难道是我的代码有问题
1
Imports System
2
Imports System.Collections
3
Imports System.Collections.Generic
4
5
Public Delegate Sub Method()
6
7
Public Module VBGeneric
8
9
Sub Main()
10
RunTest(New Test("ArrayList of string", AddressOf t1), _
11
New Test("ArrayList of int", AddressOf t2), _
12
New Test("List(Of String)", AddressOf t3), _
13
New Test("List(Of Integer)", AddressOf t4))
14
'Console.ReadLine()
15
End Sub
16
17
Sub t1()
18
Dim list As ArrayList = New ArrayList
19
For i As Integer = 0 To 1000
20
list.Add("hello")
21
Dim str As String = CType(list(0), String)
22
list.Remove(0)
23
Next
24
End Sub
25
26
Sub t2()
27
Dim list As ArrayList = New ArrayList
28
For i As Integer = 0 To 1000
29
list.Add(123)
30
Dim str As Integer = CType(list(0), Integer)
31
list.Remove(0)
32
Next
33
End Sub
34
35
Sub t3()
36
Dim list As List(Of String) = New List(Of String)
37
For i As Integer = 0 To 1000
38
list.Add("hello")
39
Dim str As String = list(0)
40
list.Remove(0)
41
Next
42
End Sub
43
44
Sub t4()
45
Dim list As List(Of Integer) = New List(Of Integer)
46
For i As Integer = 0 To 1000
47
list.Add(123)
48
Dim str As Integer = list(0)
49
list.Remove(0)
50
Next
51
End Sub
52
53
Sub RunTest(ByVal ParamArray tests() As Test)
54
For index As Integer = 0 To tests.Length - 1
55
tests(index).m_code()
56
Next
57
58
For index As Integer = 0 To tests.Length - 1
59
Dim start As DateTime
60
For m As Integer = 0 To 10
61
tests(index).m_code()
62
Next
63
Dim time As TimeSpan = DateTime.Now - start
64
DrawBar(index, time, tests(index).m_Name)
65
Next
66
67
End Sub
68
69
Sub DrawBar(ByVal index As Integer, ByVal time As TimeSpan, ByVal text As String)
70
71
Dim bg As ConsoleColor = Console.BackgroundColor
72
Dim fg As ConsoleColor = Console.ForegroundColor
73
If index Mod 2 = 0 Then
74
Console.BackgroundColor = ConsoleColor.DarkBlue
75
Else
76
Console.BackgroundColor = ConsoleColor.DarkRed
77
End If
78
Console.ForegroundColor = ConsoleColor.White
79
Dim size As Integer = Math.Min(CType((time.Milliseconds / 10), Integer), 79)
80
81
text = " " + text + " " + time.Milliseconds.ToString() + "ms"
82
83
For i As Integer = 0 To size
84
Console.Write(" ")
85
Next
86
Console.WriteLine()
87
88
For i As Integer = 0 To size
89
If i < text.Length Then
90
Console.Write(text(i))
91
Else
92
Console.Write(" ")
93
End If
94
95
Next
96
Console.WriteLine()
97
98
For i As Integer = 0 To size
99
Console.Write(" ")
100
Next
101
102
Console.WriteLine()
103
Console.WriteLine()
104
Console.BackgroundColor = bg
105
Console.ForegroundColor = fg
106
End Sub
107
End Module
108
109
Public Class Test
110
111
Public m_Name As String
112
Public m_code As Method
113
114
Public Sub New(ByVal name As String, ByVal code As Method)
115
Me.m_Name = name
116
Me.m_code = code
117
End Sub
118
119
End Class
Imports System2
Imports System.Collections3
Imports System.Collections.Generic4

5
Public Delegate Sub Method()6

7
Public Module VBGeneric8

9
Sub Main()10
RunTest(New Test("ArrayList of string", AddressOf t1), _11
New Test("ArrayList of int", AddressOf t2), _12
New Test("List(Of String)", AddressOf t3), _13
New Test("List(Of Integer)", AddressOf t4))14
'Console.ReadLine()15
End Sub16

17
Sub t1()18
Dim list As ArrayList = New ArrayList19
For i As Integer = 0 To 100020
list.Add("hello")21
Dim str As String = CType(list(0), String)22
list.Remove(0)23
Next24
End Sub25

26
Sub t2()27
Dim list As ArrayList = New ArrayList28
For i As Integer = 0 To 100029
list.Add(123)30
Dim str As Integer = CType(list(0), Integer)31
list.Remove(0)32
Next33
End Sub34

35
Sub t3()36
Dim list As List(Of String) = New List(Of String)37
For i As Integer = 0 To 100038
list.Add("hello")39
Dim str As String = list(0)40
list.Remove(0)41
Next42
End Sub43

44
Sub t4()45
Dim list As List(Of Integer) = New List(Of Integer)46
For i As Integer = 0 To 100047
list.Add(123)48
Dim str As Integer = list(0)49
list.Remove(0)50
Next51
End Sub52

53
Sub RunTest(ByVal ParamArray tests() As Test)54
For index As Integer = 0 To tests.Length - 155
tests(index).m_code()56
Next57

58
For index As Integer = 0 To tests.Length - 159
Dim start As DateTime60
For m As Integer = 0 To 1061
tests(index).m_code()62
Next63
Dim time As TimeSpan = DateTime.Now - start64
DrawBar(index, time, tests(index).m_Name)65
Next66

67
End Sub68

69
Sub DrawBar(ByVal index As Integer, ByVal time As TimeSpan, ByVal text As String)70

71
Dim bg As ConsoleColor = Console.BackgroundColor72
Dim fg As ConsoleColor = Console.ForegroundColor73
If index Mod 2 = 0 Then74
Console.BackgroundColor = ConsoleColor.DarkBlue75
Else76
Console.BackgroundColor = ConsoleColor.DarkRed77
End If78
Console.ForegroundColor = ConsoleColor.White79
Dim size As Integer = Math.Min(CType((time.Milliseconds / 10), Integer), 79)80

81
text = " " + text + " " + time.Milliseconds.ToString() + "ms"82

83
For i As Integer = 0 To size84
Console.Write(" ")85
Next86
Console.WriteLine()87

88
For i As Integer = 0 To size89
If i < text.Length Then90
Console.Write(text(i))91
Else92
Console.Write(" ")93
End If94

95
Next96
Console.WriteLine()97

98
For i As Integer = 0 To size99
Console.Write(" ")100
Next101

102
Console.WriteLine()103
Console.WriteLine()104
Console.BackgroundColor = bg105
Console.ForegroundColor = fg106
End Sub107
End Module108

109
Public Class Test110

111
Public m_Name As String112
Public m_code As Method113

114
Public Sub New(ByVal name As String, ByVal code As Method)115
Me.m_Name = name116
Me.m_code = code117
End Sub118

119
End Class


浙公网安备 33010602011771号