Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Performance problem stripping blanks

Status
Not open for further replies.

Capoerista

Programmer
Aug 13, 2002
44
GB
Hi All,

I've inherited support of a VB application which is having performance problems processing large text files. I have narrowed down the problem to the for loop section. The aim is to read in an rtf file, then output a copy of the file with rightmost blanks stripped.

The routine is

Code:
Function TrimTrailingBlankLines(Indat As String) As String
Dim Res As String
        Dim lins, ln, Lin

    Debug.Print "TrimTrailing... split"

        lins = Split(Trim(Indat), vbCrLf)
        
    Debug.Print "TrimTrailing... for"
        
        For ln = UBound(lins) To 0 Step -1
            Lin = lins(ln)
            If Trim(left(Lin, 4)) = "____"
            Then
                Lin = Space(Len(Lin))
            End If
            If Len(Res) = 0 Then
                If Trim(Lin) <> "" Then
                    Res = Lin
                End If
            Else
                Res = Lin & vbCrLf & Res
            End If
        Next
    Debug.Print "... TrimTrailing"

    TrimTrailingBlankLines = Res
End Function

For context the code that calls this routine is

Code:
  Debug.Print "Read form frmEmail_2_OTC"
   Load frmEmail_2_OTC
   Set TS = fso.OpenTextFile(INPUT_FILE, ForReading)
   frmEmail_2_OTC.rtfTemp.TextRTF = TS.ReadAll
   TS.Close
  Debug.Print "close form"
   NewIn = fso.GetParentFolderName(INPUT_FILE) & "\OrigRTF_" & fso.GetFileName(INPUT_FILE)
   If fso.FileExists(NewIn) Then
       Kill NewIn
   End If
        
   Name INPUT_FILE As NewIn
        
   'Name Run_File as
   Set TS = fso.OpenTextFile(INPUT_FILE, ForWriting, True)
        
  Debug.Print "Trim Blanks"
   TS.Write TrimTrailingBlankLines(Trim(frmEmail_2_OTC.rtfTemp.Text)) & vbCrLf
    TS.Close
   Debug.Print "End Trim Blanks"


The input file is approx 3Mb and takes about 20 mins on a 2Ghz PC with 256Mb memory to get through this routine. Is the main problem doing a len on an increasingly large string? Any pointers on possible improvements would be greatly appreciated.

Cheers

Ade

P.S. I'm not sure why the loop is going backwards through the array.
 
To strip trailing spaces on a string you can use RTrim:

Ex. -

MsgBox Len(RTrim$(" Test "))

This will give a result of 6.

Swi
 
The other bit of code that looks like it is hogging memory is Res = Lin & vbCrLf & Res.

Appending strings in VB is very slow.

Swi
 
Is this something that could be done with a regular expression? I am very new to them so I don't have any idea what it would look like...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top