String Surprise

String Surprise

I was developing a CGI application that read a database and pieced together the fields into a string forrepresentation as an HTML form. The surprise came when I found how slow it was-20 seconds wasunacceptable for the task. I first suspected the database access and thought there was no way to improve it;however, upon investigating further, it turned out to be the loop that concatenated the fields into the string. Asimple change took the run time of the routine down to about one second. In this simplified example, insteadof writing this code:

 For i = 1 To 10000        strHTML = strHTML & strField & vbTabNext i

Break it down into something like this:

 For i = 1 To 100        strTemp = ""        For j = 1 to 100                strTemp =  strTemp & strField _                        & vbTab        Next j        strHTML =  strHTML & strTemp Next i

Admittedly, the number of concatenations is high, but the difference is astounding. In the 16-bit world, thesecond example is about 20 times faster than the first on my machine. However, in VB 32-bit, my Pentium133 with 32 MB RAM takes 48 seconds for the first case and less than one second for the second case. Ihesitate to suggest a precise speed improvement because of the complexity of Windows 95 and differenthardware, but if you must concatenate large numbers of strings, this could become a make-it-or-break-itproblem.


Share the Post: