Slow code with Ubound

I am having problems with an agent slowing down considerably when it runs code using the Ubound function, especially with larger documents (>2meg). When this happens, the agent that normally runs in minutes takes hours. Can you give me any suggestions on what to do to get better perfomance? My subroutine follows:

Sub outputDocument (theHandle As Integer, theOutputDoc As NotesDocument, theColumnIdx As Integer, theDelimiter As String,theOutDocFileName As String)

	Dim i As Long

	Dim j As Long

	Dim k As Long

	Dim delim As String

	Dim s As String

	Dim itm As NotesItem

	

	On Error Goto HandleOutDocErr ' error handling to trap file already open error - jrj 05/12/05

	

	If Debug > 1 Then Print "+outputDocument     "+Str$(Loc(theHandle)) ' added file position to debug msg - jrj 05/12/05

	

	' debug msg - jrj 05/13/05

	If Debug > 1 Then 

		Print "+outputDocument.theOutputDoc.LineIdx(0) "+Str$(theOutputDoc.LineIdx (0)) 

		Print "+outputDocument.theOutputDoc.theColumnIdx "+Str$(theColumnIdx)

	End If

	

	For i = 0 To theOutputDoc.LineIdx (0) 

		For j = 0 To theColumnIdx

			Set itm = theOutputDoc.GetFirstItem ("Column" & Cstr (j+1))

			

			 'change to reduce references to itm.Values - jrj 05/13/05

			If i > Ubound (itm.Values) Then

			s = itm.Values (Ubound (itm.Values))

		Else

			s = itm.Values (i)

		End If

		

			If j < theColumnIdx Then

				delim = theDelimiter

			Else

				delim = Chr$ (13) & Chr$ (10)

			End If

			Print #theHandle, s & delim;

		Next

		

		If Debug > 1 Then 

			If Loc(theHandle) >= 114688 Then ' dont print this debug unless at least 14mb has been written

				Print "-outputDocument.end of j loop "+Str$(Loc(theHandle)) ' jrj 05/13/05		

			End If

		End If

	Next

	

	' close and reopen output file to prevent File Already Open error - jrj 05/12/05

	

	Close theHandle

	Open theOutDocFileName For Append As theHandle

	

	If Debug > 1 Then Print "-outputDocument     "+Str$(Loc(theHandle)) ' added file position to debug msg - jrj 05/12/05		

	

	Exit Sub

Subject: Slow code with Ubound

Your ubound calls need need be in the form Ubound(itm), not Ubound(itm.Values). Ubound(itm) will give you the upper bound for the itm array.

Douglas

Subject: Slow code with Ubound

The main thing that seems likely (assuming I understand any of the scenario) is to compute Ubound (itm.Values) outside of the loop rather than having the Ubound happen thousands of times.

Am I close? This subroutine is reading a 14 Mb document, parsing thousands of lines each with multiple fields (“Column1”, “Column2”, …), and you are iterating through this array. After reading the whole document it appends something to the end. Sounds like that might be slow.

p.s.

On this line:

If Loc(theHandle) >= 114688 Then ' dont print this debug unless at least 14mb has been written

114688 cannot equal 14 Mb.

Also, I wonder about the code here:

For i = 0 To theOutputDoc.LineIdx (0)

For j = 0 To theColumnIdx

Should that not have “theColumnIdx(0)”?