user1804925
user1804925

Reputation: 159

Reduce performance time

  'Check if the file has any broken records
        Dim reader As StreamReader = New StreamReader(fileDirectory)
        Dim fileLine As String
        Dim stopCheck As Boolean = False

        Do While reader.Peek() > -1
            fileLine = reader.ReadLine()

           'If the line is not start with eCW| then it is a broken record
            If Not fileLine.StartsWith("eCW|") Then
                stopCheck = True
                Exit Do
            End If
        Loop

        reader.Close()

        If stopCheck Then

It will take a very long time to validate the rows when the text file has many records.

ex. File 1 has 500,000 completed records. It will looping through all the rows until it close the program.

File 2 has a broken records toward the end of text file. It will have to looping through all the rows before it find the broken record.

Is there a way to speed up this validation process?

Upvotes: 0

Views: 82

Answers (2)

Rob
Rob

Reputation: 3863

Just winging it here but ...

   try
       while not reader.readline().startswith("eCWL")
       end while
       stopCheck = true
    catch
       stopCheck = false
    end try

I'm guessing the catching the read exception when you try to read beyond eof will be well worth not needing to do the peek and the read for each record

Upvotes: 1

JerryM
JerryM

Reputation: 885

Eliminating the use of the fileLine variable will eliminate a memory allocation on each iteration.

    Do While reader.Peek() > -1
       'If the line is not start with eCW| then it is a broken record
        If Not reader.ReadLine().StartsWith("eCW|") Then
            stopCheck = True
            Exit Do
        End If
    Loop

Since strings are immutable, a memory allocation takes place each time the string is changed. Link Other than this, I think your approach is great--once you add the Using statement that is.

Upvotes: 1

Related Questions