Sunday, 04 July, 2004

Here's a problem. You want to concatenate a large number of strings (think: database generated list-box) quickly in ASP?

Like most scripting languages vbscript is bad at concatenations if you use the naive method. Consider the following code snippet:

A="This is a sentence."

for C = 1 to 5000
   B = B & A	
next 

Response.write B

The way the interpreter does the concatenation is it takes the memory location for B and A, makes a copy of the data at those locations and copies it to a new bit of memory. It then reassigns the pointer for the 'B' variable to this new location. This way of doing things is slow. It takes around 2.2 seconds to complete this loop on my laptop.

A better way is to use an array and then use the vbscript join function. This function takes a series of strings stored in an array and concatenates them together. It's a vast improvement. Consider the following code snippet that reproduces function above:

dim Sentences(4999)
			
TheSentence = "This is a sentence."

for i = 0 to 4999
   Sentences(i)=TheSentence
next
			
response.write join(Sentences,"")

This function has a bit more grunt. It completes the same job in around an eight hundredth of a second. That's 30x faster than the method above!

I continued the investigation to see just how quickly the performance of the concatenate operator degrades as the number of iterations grows. For comparison, I plotted this against the performance degradation of the array based method. I was stunned to learn that the '&' operator's time grows exponentially with the number of iterations. In contrast, the array method's execution time grows linearly with an increase in the number of iterations.

Graph showing the performance difference between the join and naieve methods of string concatenation

Cheers,

Simon.

22:39:14 GMT | #Programming | Permalink
XML View Previous Posts