如何在vb.net中提高循环速度?

huangapple go评论120阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 18:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定