Often when using ASP, you find yourself preparing long strings of
results which will eventually be written out to the browser. There
are several ways to do this, but the two most basic are to simply
Response.Write the text out to the browser as you build it or
to build a longer string variable and output it all once you're
done building it. One of these methods is much more efficient
then the other one and this article will tell you which one and why.
The Basic Concept
The easiest way to compare the efficeincy of two scripts is to
test them against each other and see which runs faster and uses
fewer resources. Well, since what we're testing runs so quickly,
we're going to need to do it a lot to see any difference.
The scripts do exactly the same thing. They output to the browser
a sentence repeated a given number of times. One outputs the sentence
each time through the loop and the other wait till the end and then
outputs the entire page.
The Scripts
Since there's really not much to them, I'll skip the
"chit-chat" and just give you the scripts.
Oh and don't get thrown by the XHTML stuff... they're
still just standard web pages.
output_performance_string_building.asp
<%@ Language="VBScript" %>
<% Option Explicit %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Output Performance - String Building</title>
<meta name="author" content="John Peterson" />
</head>
<body bgcolor="#FFFFFF">
<%
Const LOOPS = 1000
Dim iTimerStart, iTimerEnd, iTimer
Dim I
Dim strTemp
iTimerStart = Timer()
Response.Write "<p>"
For I = 1 To LOOPS
strTemp = strTemp & "The quick brown fox jumps over the lazy dog. "
Next 'I
Response.Write strTemp
Response.Write "</p>"
iTimerEnd = Timer()
iTimer = iTimerEnd - iTimerStart
Response.Write "<h1>" & iTimer & "</h1>"
%>
</body>
</html>
output_performance_response_write.asp
<%@ Language="VBScript" %>
<% Option Explicit %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Output Performance - Response.Write </title>
<meta name="author" content="John Peterson" />
</head>
<body bgcolor="#FFFFFF">
<%
Const LOOPS = 1000
Dim iTimerStart, iTimerEnd, iTimer
Dim I
Dim strTemp
iTimerStart = Timer()
Response.Write "<p>"
For I = 1 To LOOPS
Response.Write "The quick brown fox jumps over the lazy dog. "
Next 'I
Response.Write ""
Response.Write "</p>"
iTimerEnd = Timer()
iTimer = iTimerEnd - iTimerStart
Response.Write "<h1>" & iTimer & "</h1>"
%>
</body>
</html>
As you can see they're almost identical and the only
difference between them (besides the titles) is where
the Response.Write commands to send the text to the
browser are located. If you want to see the comparison
click here.
Testing
I did the comparison of the scripts on my laptop. (For
those of you who care, my laptop is PIII 500 with 192MB
of Memory running Win 2000 Pro.) The web
server was running on the same machine to make sure
bandwidth issues didn't get involved as we got into the
higher number of repetitions. (These files can get big...
the 5000 repetitions with 2 sentences resulted in an HTML
file almost 500kb in size.)
I ran the first set using the scripts as they are listed.
I varied the LOOPS constant from 500 to 5000 in increments
of 500. After that I doubled the size of the text string
I was using so that it contained the sentence twice. (I did
this based on my previous knowledge that string concatination
in VBScript is slow and I wanted to confirm and illustrate that
the concatenation of larger strings increased the delay.)
Results
One Sentence
Two Sentences
Repetitions
String Building
Response.Write
String Building
Response.Write
500
0.01953125
0.00781250
0.03906250
0.00000000
1000
0.08203125
0.00781250
0.48046880
0.00781250
1500
0.33203130
0.01171875
2.06250000
0.01171875
2000
0.94140630
0.00781250
4.23437500
0.00781250
2500
2.28125000
0.01171875
6.33984400
0.01171875
3000
4.31640600
0.01171875
9.82421900
0.01171875
3500
5.96875000
0.01171875
13.23047000
0.01953125
4000
8.32421900
0.01171875
17.36719000
0.01953125
4500
10.69531000
0.01953125
22.85156000
0.01953125
5000
13.51172000
0.03125000
28.09766000
0.01953125
If you look carefully at the numbers above, it becomes obvious that a number of them look very
similar... this is due to the lack of precision with which VBScript measures time.
While this would be unacceptable for real data, for our purposes it's close enough
and while we may not be able to get any hard numbers on how much faster Response.Write
is when compared to String Building, it's easy to determine which one is faster.
Just to make the point painfully clear, I threw the numbers into an Excel sheet and whipped
up a little graph to illustrate. Since we're talking about time to render the page,
lower numbers (and lines!) are better.
Conclusion
While I certainly fabricated the situation somewhat to illustrate my point, I think
we've pretty clearly illustrated that if you need to build and return a lot
of text to the browser, it's much better to Response.Write it out as you go
along then to build a long string to output later. Will this make all your
scripts run faster or eliminate all your performance problems? Of course
not, but it certainly can't hurt... especially in all those loops you've got
to build your tables.
If you'd like to experiment with the scripts or do some testing of your own,
you can download the scripts used in this article from here.
I've even included a hybrid script which combines the two methods and
produces some interesting results of it's own.