0

我正在读取输出到 textview 的 1-5mb 日志文件,并搜索输出到另一个 textview 的特定行。目前,一个 1mb 的文件大约需要一分钟。除了我正在使用的方法之外,有没有人知道任何更快的搜索行或字符串的方法?

Imports EnterpriseDT.Net.Ftp

Public Class Form1

Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim sw As New Stopwatch
    Dim FullLine As String = ""
    Dim ScriptLine As String = ""
    sw.Start()

    Dim ll As New Queue(Of String)

    Dim i As String = ""

    Using TestFile As New IO.StreamReader("c:\test.txt", System.Text.Encoding.Default, False, 4096)

        Using OutFile As New IO.StreamWriter("c:\SBOutFile.txt", False, System.Text.Encoding.Default, 4096)

            While TestFile.EndOfStream = False

                i = TestFile.ReadLine

                If i.Contains(".sqf") And i.Contains("handleGear.sqf") = False Then
                    ScriptLine = ScriptLine & i & vbNewLine & vbNewLine
                    FullLine = FullLine & i & vbNewLine & vbNewLine
                Else
                    FullLine = FullLine & i & vbNewLine & vbNewLine
                End If

            End While

        End Using

    End Using

    sw.Stop()
    TextBox1.Text = FullLine
    TextBox2.Text = ScriptLine
    RichTextBox1.AppendText(String.Format("Run_Queue took {0} Milliseconds." & Environment.NewLine, sw.ElapsedMilliseconds))
End Sub

Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try
        'connect to ftp server
        Dim ftp As New FTPConnection
        ftp.ServerAddress = "-"
        ftp.ServerPort = "-"
        ftp.UserName = "-"
        ftp.Password = "-"
        ftp.Connect()
        ftp.ChangeWorkingDirectory("-")
        ftp.TransferType = FTPTransferType.BINARY

        'download a file
        ftp.DownloadFile("c:\test.txt", "scripts.log")

        'ftp.RenameFile("scripts.log", "scripts_test.log")

        'close the connection
        ftp.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

End Sub

结束类

4

2 回答 2

1

鉴于您在阅读时所做的大量阅读和连接,我怀疑这是您的时间/性能问题的一部分。我可能会考虑将 ScriptLine 和 FullLine 的声明从 String 类型更改为 StringBuilder,因为字符串在技术上是不可变的。这意味着每个连接实际上都是对前一个对象的拆解,并在其位置创建一个新对象。StringBuilders 专为重串联场景而设计。循环完成后,您可以将其转换回字符串。

此外,编译的正则表达式可能比 String.Contains 搜索得更快。您的正则表达式字符串类似于“(?!handleGear).sqf”,意思是“在字符串“.sqf”之前“查找除“handleGear”之外的任何零个或多个字符序列。

我还没有机会测试该表达式,因此提供了该警告。如果我有机会进行测试,我会很高兴修改并让您知道。

祝你好运!

于 2012-07-11T20:17:40.643 回答
0

我只是想发布我最后想出的东西。这是对我所拥有的非常大的改进。

'Read file
    Dim sw As New Stopwatch
    Dim FullLine As String = ""
    Dim ScriptLine As String = ""
    sw.Start()

    Dim ll As New Queue(Of String)

    Dim i As String = ""
    Dim builder As New StringBuilder
    Using TestFile As New IO.StreamReader("c:\test.txt", System.Text.Encoding.Default, False, 4096)

        builder.AppendLine("Started at: " & DateTime.Now.ToLongTimeString().ToString)
        RichTextBox1.AppendText(Now.ToShortTimeString & " Reading Log File Started" & vbNewLine)
        RichTextBox1.SelectionStart = RichTextBox1.TextLength

        Dim rowCount As Integer = 0

        Do Until TestFile.EndOfStream
            ScriptLine = TestFile.ReadLine
            ScriptLine = LCase(ScriptLine)
            If InStr(ScriptLine, ".sqf") > 0 And InStr(ScriptLine, "handlegear.sqf") < 1 Then 'And InStr(ScriptLine, "createmarkerlocal.sqf") < 1 And InStr(ScriptLine, "setmarkerposlocal.sqf") < 1 
                builder.AppendLine(ScriptLine)
                builder.AppendLine()
            End If
            rowCount = rowCount + 1
        Loop
        builder.AppendLine(Now.ToShortTimeString & "==== Searched " & rowCount & " rows" & vbNewLine)
        builder.AppendLine(Now.ToShortTimeString & " Finished" & vbNewLine)
    End Using

    sw.Stop()

    RichTextBox2.AppendText(builder.ToString & vbNewLine)

    RichTextBox1.AppendText(Now.ToShortTimeString & String.Format(" Run_Queue took {0} Milliseconds." & Environment.NewLine, sw.ElapsedMilliseconds))
于 2012-07-18T15:54:08.557 回答