Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 7, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

String Surprise

I was developing a CGI application that read a database and pieced together the fields into a string for representation as an HTML form. The surprise came when I found how slow it was-20 seconds was unacceptable 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. A simple change took the run time of the routine down to about one second. In this simplified example, instead of writing this code:
For i = 1 To 10000
        strHTML = strHTML & strField & vbTab
Next 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, the second example is about 20 times faster than the first on my machine. However, in VB 32-bit, my Pentium 133 with 32 MB RAM takes 48 seconds for the first case and less than one second for the second case. I hesitate to suggest a precise speed improvement because of the complexity of Windows 95 and different hardware, but if you must concatenate large numbers of strings, this could become a make-it-or-break-it problem.
Nick Snowdon
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date