ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
Improve efficiency with server-side includes
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Output Performance - String Building vs. Response.Write

Output Performance - String Building vs. Response.Write

by John Peterson

Introduction

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 SentenceTwo Sentences
RepetitionsString BuildingResponse.WriteString BuildingResponse.Write
5000.019531250.007812500.039062500.00000000
10000.082031250.007812500.480468800.00781250
15000.332031300.011718752.062500000.01171875
20000.941406300.007812504.234375000.00781250
25002.281250000.011718756.339844000.01171875
30004.316406000.011718759.824219000.01171875
35005.968750000.0117187513.230470000.01953125
40008.324219000.0117187517.367190000.01953125
450010.695310000.0195312522.851560000.01953125
500013.511720000.0312500028.097660000.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.

Output Performance Comparison Graph

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.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers