VB.net循环需要8秒,我觉得很慢

时间:2016-10-01 04:45:05

标签: vb.net for-loop foreach visual-studio-2015

我之前发过这样的问题,但即使有回复,我仍然无法让代码更快。我现在已经修改了代码,并且正在寻找有关如何加快代码的建议。

我有一个300k行的richtextbox,当每个循环到达包含30k行的richtextbox的某一行时,我需要替换一个文本块。 (如果未在此代码中声明变量,则表示它们是全局变量)

这是我的代码:

Dim counter As Integer = 0
        Dim startcount As Boolean = False
        Dim holdstring As String = ""


        Dim completertb4 As New StringBuilder("")
        Dim completertb4one As New StringBuilder("")
        MsgBox(newrtb.Lines.Count,, "")
        Dim counteroflines As Integer = 1
        For Each line As String In newrtb.Lines

            'MsgBox(counteroflines & "----" & holdstring,, "")

            If line.Contains(richtextboxl0) Then
                startcount = True
                holdstring = vbNewLine & richtextbox3text

            ElseIf startcount = True Then
                If counter = temptextbox3count Then
                    startcount = False
                Else
                    counter += 1
                End If

            ElseIf completertb4.ToString = "" Then
                holdstring = line
            Else
                holdstring = line

            End If

            If counteroflines >= 1 And counteroflines <= 10000 Then

                completertb4.Append(vbNewLine & holdstring & "----" & counteroflines)
            Else

                completertb4one.Append(vbNewLine & holdstring & "----" & counteroflines)
            End If

            counteroflines += 1
        Next

1 个答案:

答案 0 :(得分:0)

以下是我从08:00秒到00:01秒缩小代码的方式。从8秒到不到一秒钟。

            Dim counter As Integer = 0
        Dim countertrue As Integer = 0
        Dim countintertal As Integer = 0


        Dim newrtbstrarray As String() = newrtb.Lines
        Dim rtb3array As String() = richtextbox3text.Lines

        For Each line As String In newrtbstrarray

            If line.Contains(richtextboxl0) Then
                countertrue = counter
                For Each element As String In rtb3array
                    newrtbstrarray(countertrue) = rtb3array(countintertal)
                    countertrue += 1
                    countintertal += 1
                Next

            End If

            counter += 1
        Next