The most efficient way to write ASP-generated content to the response stream

Source: Internet
Author: User
Tags date connect wrapper
When you first started to learn ASP, the HTML in the book contains ASP
Later, output HTML with Response.Write
But now read this article, do you really want to write a sentence of Response.Write ("& _?
You tell me?
-------
(Turn http://www.ccidnet.com/html//tech/web/2000/11/10/58_1298.html)
What is the best choice for improving ASP performance (i)
(Author: Green Apple studio compiled November 10, 2000 17:20)

What is the most effective way to write ASP-generated content to the response stream?
One of the most important reasons for using ASP is to generate dynamic content on the server. So obviously, the starting point of our testing is to determine the most appropriate way to send dynamic content to the response stream. Of the many choices, two are most basic: one is using inline ASP tags and the other is using Response.Write statements.

To test these choices, we created a simple ASP page that defines the variables and then inserts their values into the table. Although this page is very simple and not very practical, it allows us to isolate and test some separate issues.

Using ASP inline markup

The first test includes the use of inline ASP tags <%= x, where x is an assigned variable. So far, this method is easiest to perform, and it keeps the HTML part of the page in a format that is easy 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 &GT;&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 &GT;&L T /tr >

< TR >< TD >< B >EMail:</b ></td >< TD ><%= EMail% ></td ></tr &G T

< TR >< TD >< B >birth date:</></td >< TD ><%= birthdate% ></td &GT;&L T /tr >

</table >

</body >


Complete code for/app1/response1.asp

Previous best (reaction speed) = 8.28 msec/page

Use the Response.Write statement on every line in the HTML

Many of the better learning documents suggest avoiding using the previous approach. The main reason for this is that in the process of applying reaction time to the output page and the processing page, a problem called context conversion occurs if the Web server has to convert between sending pure HTML and processing scripts. When most programmers hear this, their first reaction is to wrap each line of the original HTML in 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 >")

...

Fragments of/app1/response2.asp

Previous best (reaction speed) = 8.28 msec/page

Reaction time = 8.08 Msec/page

difference = -0.20 msec (Decrease 2.4%)

We can see that this is a very small gain in performance compared to using inline tags, perhaps because the page loads a large bunch of small function calls to the server. The biggest disadvantage of this approach is that since HTML is now embedded in the script, the scripting code becomes more verbose and harder to read and maintain.

Using wrapper functions

When we try to use the Response.Write statement this method, the most frustrating discovery may be that the Response.Write function cannot place a CRLF at the end of each line. So when you read the source code from the browser, the HTML, which was nicely decorated, is now a line that has no end. I think your next discovery may be more frightening: there is no sister function Writeln in the response object. So, one obvious response is to create a wrapper function for the Response.Write function to attach a CRLF to each row.

...

WRITECR ("< tr >< TD >< B >first name:</></td >< td >" & FirstName & "< /td ></tr > ")

...

SUB WRITECR (str)

Response.Write (Str & VbCRLF)

End SUB

Fragments of/app1/response4.asp

Previous best (reaction speed) = 8.08 msec/page

Reaction time = 10.11 Msec/page

difference = +2.03 msec (Increase 25.1%)

Of course, since this method effectively doubles the number of function calls, its impact on performance is obvious, so avoid at all costs. Ironically, Crlf also added 2 bytes to the reaction flow for each line, which browsers do not need to render to the page. All you can do with well-formed HTML is make it easier for your competitors to read your HTML source code and understand your design.

Connecting consecutive Response.Write to a single statement

Regardless of the test we used in the wrapper function, the next logical step is to extract all the strings from a separate Response.Write statement and connect them to a single statement, which reduces the number of function calls and greatly improves the performance of the page.

...

Response.Write ("< HTML >" & _

"< head >" & _

"< title >response test</title >" & _

"
"< BODY >" & _

"< H1 >response test


"< table >" & _

"< TR >< TD >< B >first name:</></td >< td >" & FirstName & "</td > </tr > "& _

...

"< TR >< TD >< B >birth date:</></td >< td >" & Birthdate & "</td > </tr > "& _

"</table >" & _

"</body >" & _

"
Fragments of/app1/response3.asp

Previous best (reaction speed) = 8.08 msec/page

Reaction time = 7.05 Msec/page

difference = -1.03 msec (Decrease 12.7%)

Currently, this is the most optimized configuration.

Connect consecutive Response.Write to a single statement, adding a CRLF at the end of each line

Given the sheer number of people who require their source code to be seen from the browser, I used the vbCrLf constant to insert some carriage returns at the end of each line in the previous test, and then rerun.

...

Response.Write ("< HTML >" & VbCRLF & _

"< head >" & VbCRLF & _

"< title >response test</title >" & VbCRLF & _

"
...

Fragments of/app1/response5.asp

Front of the best (reaction speed) = 7.05 msec/page

Reaction time = 7.63 Msec/page

difference = +0.58 msec (Increase 8.5%)

The results of the run are a bit lower in performance, perhaps due to additional concatenation and increased character volume.

Review and observation

Some rules can be drawn from the previous tests on ASP output:

* Avoid excessive use of inline ASP.

* Always concatenate consecutive Response.Write statements into a single statement.

* Never use wrapper functions around Response.Write to attach CRLF.

* If you must format the HTML output, attach the CRLF directly within the Response.Write statement.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.