我的代码在下面,我需要从sub更快的是如果数组A中的第一个值与A中的每个其他值相同,我可以通过键入一些大的if语句来做到但我宁愿不这样做。
提前致谢
Public Function Areas_Of_England_Checker()
Dim South_East() As System.Drawing.Color = {Oxfordshire.BackColor, Buckinhamshire.BackColor, Berkshire.BackColor, Hampshire.BackColor, Isle_Of_White.BackColor, West_Sussex.BackColor, Kent.BackColor, Surrey.BackColor, London.BackColor}
Dim South_West() As System.Drawing.Color = {Devon.BackColor, Cornwall.BackColor, Somerset.BackColor, Whiltshire.BackColor, Gloucestershire.BackColor, Dorset.BackColor}
Dim East_Anglia() As System.Drawing.Color = {Hertfordshire.BackColor, Bedfordshire.BackColor, Cambridgeshire.BackColor, Essex.BackColor, Suffolk.BackColor, Norfolk.BackColor, Northamptonshire.BackColor}
Dim East_Midlands() As System.Drawing.Color = {Lincolnshire.BackColor, Nottinghamshire.BackColor, Leicestershire.BackColor, Derbyshire.BackColor}
Dim West_Midlands() As System.Drawing.Color = {Shropshire.BackColor, Staffordshire.BackColor, Herefordshire.BackColor, Worcestershire.BackColor, Warwickshire.BackColor, Cheshire.BackColor}
Dim North_West() As System.Drawing.Color = {Manchester.BackColor, Merseyside.BackColor, Lancashire.BackColor, Cumbria.BackColor}
Dim Yorkshire() As System.Drawing.Color = {North_Yorkshire.BackColor, West_Yorkshire.BackColor, South_Yorkshire.BackColor, East_Yorkshire.BackColor}
Dim North_East() As System.Drawing.Color = {Northumberland.BackColor, County_Durham.BackColor}
quicker(South_East, 9)
quicker(South_West, 6)
quicker(East_Anglia, 7)
quicker(East_Midlands, 6)
quicker(North_West, 4)
quicker(Yorkshire, 4)
quicker(North_East, 2)
End Function
Public Sub quicker(A As Array, B As Integer)
If A(0) Is the same as any value in A Then ' sort-of pseudo-code
resupplytroops += B
End If
End Sub
答案 0 :(得分:3)
问题文本和示例代码之间存在断开连接。我会回答这两个选项。这基于示例代码,其中数组中的任何值可以匹配第一个值:
Public Sub quicker(A() As System.Drawing.Color, B As Integer)
' If A(0) Is the same as any other value in A
If A.Skip(1).Any(Function(i) i.Equals(A(0))) Then
resupplytroops += B
End If
End Sub
基于问题文本,其中 all 数组中的值必须与第一个值匹配:
Public Sub quicker(A() As System.Drawing.Color, B As Integer)
If A.All(Function(i) i.Equals(A(0))) Then
resupplytroops += B
End If
End Sub
答案 1 :(得分:0)
快速执行扫描数组...
Public Sub quicker(A As Array, B As Integer)
If IsSame(A) Then ' sort-of pseudo-code
resupplytroops += B
End If
End Sub
Private Function IsSame(A As Array) As Boolean
For I as Integer = 1 to A.Length - 1
If A(I) <> A(0) Then Return False
Next
Return True
End Function
答案 2 :(得分:0)
FYI这是我的LinQ vs In-Line Scan时间基准测试代码和结果。
Public Sub LinqVsScanTest()
Dim A() As System.Drawing.Color
ReDim A(1000000)
A.Initialize()
A(1000000) = Color.Blue
Dim ST As Double
Dim MinT As Double = Double.MaxValue
Dim MaxT As Double = 0
Dim C As Color = A(0)
For l = 1 To 1000
Dim T As Date = Now
Dim B As Boolean = A.All(Function(i) i = C)
Dim DT As Double = Delta_MilliSeconds(T, Now)
ST += DT
If DT > MaxT Then MaxT = DT
If DT < MinT Then MinT = DT
Next
Debug.Print("LinQ took - Average :" & ST / 1000 & " Min :" & MinT & " Max :" & MaxT)
ST = 0
MinT = Double.MaxValue
MaxT = 0
For l = 1 To 1000
Dim T As Date = Now
Dim B As Boolean = True
For x As Integer = 1 To A.Length - 1
If A(x) <> A(0) Then B = False : Exit For
Next
Dim DT As Double = Delta_MilliSeconds(T, Now)
ST += DT
If DT > MaxT Then MaxT = DT
If DT < MinT Then MinT = DT
Next
Debug.Print("Scan took - Average :" & ST / 1000 & " Min :" & MinT & " Max :" & MaxT)
End Sub
<强>结果
LinQ采取 - 平均值:23.640000144951最小值:21.9999812543392最大值:36.0004836693406
扫描拍摄 - 平均值:7.11899993475527最小值:5.99976629018784最大值:9.00027807801962
所有时间都以毫秒为单位。
需要记住的东西..编码快捷方式&lt;&gt;编码效率。