英文:
How to increase the speed in loop in vb.net?
问题
我有一段代码,执行时间几乎需要8秒,我想将其优化到不到1秒。欢迎提供提高速度的任何建议。
Parallel.ForEach(gOutputElements_2D,
Sub(number)
number.Stress1 = LC.stresses.Find(Function(c) c.Tag0D = number.FirstNode.Tag AndAlso c.Tag2D = number.Tag)
number.Stress2 = LC.stresses.Find(Function(c) c.Tag0D = number.SecondNode.Tag AndAlso c.Tag2D = number.Tag)
number.Stress3 = LC.stresses.Find(Function(c) c.Tag0D = number.ThirdNode.Tag AndAlso c.Tag2D = number.Tag)
End Sub)
gOutputElements_2D 是一个包含大约10000个项目的列表,而 LC.stresses 包含大约30000个项目。
英文:
I have a code which is taking almost 8 sec to execute, I want to do it below 1 sec. Any tips to increase the speed will be highly appreciated.
Parallel.ForEach(gOutputElements_2D,
Sub(number)
number.Stress1 = LC.stresses.Find(Function(c) c.Tag0D = number.FirstNode.Tag AndAlso c.Tag2D = number.Tag)
number.Stress2 = LC.stresses.Find(Function(c) c.Tag0D = number.SecondNode.Tag AndAlso c.Tag2D = number.Tag)
number.Stress3 = LC.stresses.Find(Function(c) c.Tag0D = number.ThirdNode.Tag AndAlso c.Tag2D = number.Tag)
End Sub)
gOutputElements_2D is a list, contains about 10000 items, while LC.stresses contains about 30000 items.
答案1
得分: 1
我怀疑你在这种情况下使用LINQ 会导致它的运行时间比应该的时间长三倍,因为你基本上有三个循环,其中你可以使用一个。你可以尝试这样做:
Parallel.ForEach(gOutputElements_2D,
Sub(number)
Dim found As Integer = 0
For Each c In LC.stresses
If c.Tag2D = number.Tag Then
If c.Tag0D = number.FirstNode.Tag AndAlso (found And 1) Then
number.Stress1 = c
found = found Or 1
End If
If c.Tag0D = number.SecondNode.Tag AndAlso (found And 2) Then
number.Stress2 = c
found = found Or 2
End If
If c.Tag0D = number.ThirdNode.Tag AndAlso (found And 4) Then
number.Stress1 = c
found = found Or 4
End If
End If
If found And 7 Then Exit For
Next
End Sub)
这应该将你的变量设置为每个找到的第一个实例,然后在找到它们全部时提前退出。
英文:
I suspect your using LINQ in this case is causing it to take three times longer than it should, since you basically have three loops where you could use one. You could try this:
Parallel.ForEach(gOutputElements_2D,
Sub(number)
Dim found As Integer = 0
For Each c In LC.stresses
If c.Tag2D = number.Tag Then
If c.Tag0D = number.FirstNode.Tag AndAlso (found And 1) Then
number.Stress1 = c
found = found Or 1
End If
If c.Tag0D = number.SecondNode.Tag AndAlso (found And 2) Then
number.Stress2 = c
found = found Or 2
End If
If c.Tag0D = number.ThirdNode.Tag AndAlso (found And 4) Then
number.Stress1 = c
found = found Or 4
End If
End If
If found And 7 Then Exit For
Next
End Sub)
This should set your variables to the first instance of each found, and then exit early when all of them are found.
答案2
得分: 1
Parallel.ForEach(gOutputElements_2D,
Sub(number)
Dim found As Integer = 0
For Each c As clsMSHStress In LC.stresses
If c.Tag2D = number.Tag Then
If c.Tag0D = number.FirstNode.Tag Then
number.Stress1 = c
found += 1
End If
If c.Tag0D = number.SecondNode.Tag Then
number.Stress2 = c
found += 1
End If
If c.Tag0D = number.ThirdNode.Tag Then
number.Stress3 = c
found += 1
End If
End If
If found = 3 Then Exit For
Next
End Sub)
我通过这个方法在大约1到2秒内达到了我的目标。
英文:
Parallel.ForEach(gOutputElements_2D,
Sub(number)
Dim found As Integer = 0
For Each c As clsMSHStress In LC.stresses
If c.Tag2D = number.Tag Then
If c.Tag0D = number.FirstNode.Tag Then
number.Stress1 = c
found += 1
End If
If c.Tag0D = number.SecondNode.Tag Then
number.Stress2 = c
found += 1
End If
If c.Tag0D = number.ThirdNode.Tag Then
number.Stress3 = c
found += 1
End If
End If
If found = 3 Then Exit For
Next
End Sub)
I achieved my goals through this in about 1 to 2 sec.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论