[thelist] ASP String Concatenation vs. Response.Write

Ryan Finley RyanF at SonicFoundry.com
Mon Feb 26 17:42:37 CST 2001


If I converted from VBScript into a compiled VB component, would that
eliminate the performance problem?  To put it another way...is string
concatenation a performance problem only in VBScript?

	Ryan Finley
	President - SurveyMonkey.com

-----Original Message-----
From: Ron White [mailto:ronwhite at members.evolt.org]
Sent: Monday, February 26, 2001 3:56 PM
To: thelist at lists.evolt.org
Subject: RE: [thelist] ASP String Concatenation vs. Response.Write


>From M$ themselves...
http://msdn.microsoft.com/workshop/server/asp/ASPtips.asp

Just happened to be on the page when I saw this mail...

Tip 20: Avoid String Concatenation in Loops
Many people build a string in a loop like this:

s = "<table>" & vbCrLf
For Each fld in rs.Fields
    s = s & " <th>" & fld.Name & "</th> "
Next

While Not rs.EOF
    s = s & vbCrLf & " <tr>"
    For Each fld in rs.Fields
        s = s & " <td>" & fld.Value & "</td> "
    Next
    s = s & " </tr>"
    rs.MoveNext
Wend

s = s & vbCrLf & "</table>" & vbCrLf
Response.Write s

There are several problems with this approach. The first is that repeatedly
concatenating a string takes quadratic time; less formally, the time that it
takes to run this loop is proportional to the square of the number of
records times the number of fields. A simpler example should make this
clearer.

s = ""
For i = Asc("A") to Asc("Z")
    s = s & Chr(i)
Next

On the first iteration, you get a one-character string, "A". On the second
iteration, VBScript has to reallocate the string and copy two characters
("AB") into s. On the third iteration, it has to reallocate s again and copy
three characters into s. On the Nth (26th) iteration, it has to reallocate
and copy N characters into s. That's a total of 1+2+3+...+N which is
N*(N+1)/2 copies.

In the recordset example above, if there were 100 records and 5 fields, the
inner loop would be executed 100*5 = 500 times and the time taken to do all
the copying and reallocation would be proportional to 500*500 = 250,000.
That's a lot of copying for a modest-sized recordset.

In this example, the code could be improved by replacing the string
concatenation with Response.Write() or inline script (<% = fld.Value %>). If
response buffering is turned on (as it should be), this will be fast, as
Response.Write just appends the data to the end of the response buffer. No
reallocation is involved and it's very efficient.

In the particular case of transforming an ADO recordset into an HTML table,
consider using GetRows or GetString.

If you concatenate strings in JScript, it is highly recommended that you use
the += operator; that is, use s += "some string", not s = s + "some string".



---------------------------------------
For unsubscribe and other options, including
the Tip Harvester and archive of TheList go to:
http://lists.evolt.org Workers of the Web, evolt ! 




More information about the thelist mailing list