Performance, ASP dynamically generated content in what way to output the most efficient?
Author: Cactus Studios This article hits: 123
Generating dynamic content on the server is one of the main reasons to use ASP, so the first test project we choose is to determine what method is best to send dynamic content to the response stream. There are two basic choices (and some of their changes): Using embedded ASP tags, using the Response.Write statement.
To test these different methods, we created a simple ASP page that first defines the variables and then inserts them into the table. Although this page is simple and has no practical purpose, it is enough to allow us to isolate and test each problem.
2.1 Using ASP Inline markup
The first test is to use the ASP's inline tag <%= x, where x is a variable. This is the easiest way to use it, and it makes the HTML part of the page easier to read and maintain.
<% OPTION EXPLICIT
Dim FirstName
Dim LastName
Dim middleinitial
Dim Address
Dim City
Dim State
Dim PhoneNumber
Dim Faxnumber
Dim EMail
Dim Birthdate
FirstName = "John"
MiddleInitial = "Q"
LastName = "Public"
Address = "Main Street"
City = "New York"
State = "NY"
PhoneNumber = "1-212-555-1234"
Faxnumber = "1-212-555-1234"
EMail = "John@public.com"
Birthdate = "1/1/1950"
% >
< HTML >
< head >
< title >response test</title >
< BODY >
< H1 >response test
< TABLE >
< TR >< TD >< B >first name:</></td >< TD ><%= FirstName% ></td >&L T /tr >
< TR >< TD >< B >middle initial:</></td >< TD ><%= middleinitial% ></t D ></tr >
< TR >< TD >< B >last name:</></td >< TD ><%= LastName% ></td >< /tr >
< TR >< TD >< B >Address:</b ></td >< TD ><%= address% ></td ></t R >
< TR >< TD >< B >City:</b ></td >< TD ><%= City% ></td ></tr >
< TR >< TD >< B >State:</b ></td >< TD ><%= State% ></td ></tr &G T
< TR >< TD >< B >phone number:</></td >< TD ><%= phonenumber% ></td &G t;</tr >
< TR >< TD >< B >fax number:</></td >< TD ><%= faxnumber% ></td >&L T /tr >
< TR >< TD >< B >EMail:</b ></td >< TD ><%= EMail% ></td ></tr &G T
< TR >< TD >< B >birth date:</></td >< TD ><%= birthdate% ></td >&L T /tr >
</table >
</body >
Complete code for/app1/response1.asp
Best record = 8.28 ms/page
2.2 Output each line of HTML code using Response.Write
Many good literature suggests that you should avoid using the previous inline tagging method because it results in an operation called context switching. This occurs when the type of code processed by the Web server changes (from pure HTML to script processing, or vice versa), and this switch takes a certain amount of time. After many programmers understand this, their first reaction is to output each line of HTML code with the Response.Write function:
...
Response.Write ("< HTML >")
Response.Write ("< head >")
Response.Write ("< title >response test</title >")
Response.Write ("Response.Write ("< body >")
Response.Write ("< H1 >response test
Response.Write ("< table >")
Response.Write ("< tr >< TD >< B >first name:</></td >< td >" & FirstName & "</td ></tr >")
Response.Write ("< tr >< TD >< B >middle initial:</></td >< td >" & Middleiniti Al & "</td ></tr >")
...
/app1/response2.asp Fragment
Best record = 8.28 ms/page
Response time = 8.08 MS/page
difference =-0.20 milliseconds (2.4% reduction)
Compared to the inline markup version, the performance improvements we see are very small and simply surprising. This may be because there are many more function calls in the page. But there's a bigger downside to this approach, because the HTML code is embedded within the function, and the scripting code becomes tedious, reading and maintenance inconvenient.
2.3 Using Encapsulation functions
Response.Write does not add CRLF (carriage return-line Feed, carriage return) to the end of the line of text, which is the most disappointing use of the above method. Although the HTML code has been nicely formatted on the server side, only a long line of code is still visible in the browser. But the disappointment is not only this problem, but people quickly find that there is no Response.writeln function that can automatically add CRLF. A natural response is to create a Response.Write wrapper function, followed by a CRLF at the end of each line:
...
WRITECR ("< tr >< TD >< B >first name:</></td >< td >" & FirstName & "< /td ></tr > ")
...
SUB WRITECR (str)
Response.Write (Str & VbCRLF)
End SUB
/A