How to enhance the performance of ASP program (3)

xiaoxiao2021-03-06  111

Tips 11: Using Response Buffering By opening "response buffering" to buffer a whole page content worth output, this will minimize the amount of data output to the browser, thereby improving overall performance. Every output is much consumed, so the less the effect is, the better the effect. TCP / IP When sending a small amount of large packet, TCP / IP is higher than that of sending a large amount of small packets, because it is slow and constantly transmitted. There are two ways to open Response Buffering. First, you can open Response Buffering throughout the application for the entire application, which is recommended in IIS4.0 and IIS5.0. In the default, response buffering is open. Secondly, on each page, you can place the following code to open Response Buffering: <% response.buffer = true> This code must be executed before any data output to the browser (that is, in any HTML The content is displayed before and before any cookie is set). Typically, open response buffering for the entire application is a good solution. After doing this, you don't have to set up the code as above each page header. A general question about opening Response Buffering is: The user must wait for the entire page to be generated before you can see the content. For a long-running page, you can set the response.buffer = false shut down buffer. Then, a good strategy is to use the response.flush method, which will output all HTML contents to the ASP to the browser. For example, after describing a 100 line of a 1,000-line table, the ASP can use Response.flush to force the output of this 100 row to the browser. At this time, the user can see the first 100 lines of data, the rest The data is being prepared to be generated. Note that about the example of the 1,000-line table output, for some browser, unless you encounter $ # @ 60; / table $ # @ 62; tag, they do not output any of the tables. If so, the table can be split into a number of tables containing a small amount of rows, and then the response.flush output is called after each table is generated. The new version of Internet Explorer displays the content after downloading the entire form, and if the list of the table is defined, the speed of generating the table will be particularly fast. Another problem with the Open Response Buffering is: When generating a very large page, it will consume very large server memory. Tip 12: Batch single line script and response.write command vbscript syntax $ # @ 60;% = expression% $ # @ 62; what is the value of the EXPRESSION. If Response Buffering is not open, each such statement will output data into the browser in the form of many small packets, which will reduce program performance. Therefore, please use the following skills: 舭ぷ 亩 鲆 泶锸 泶锸 饔 饔 饔  龅 名  肦 输 饔 输 输 输 输 输 输 输 输 输For example, in the following example, for each field of each field, there is only one write operation: $ # @ 60; table $ # @ 62;

$ # @ 60;% for each fld in @ 62;

$ # @ 60; TH $ # @ 62; $ # @ 60;% = fld.name% $ # @ 62; $ # @ 60; / th $ # @ 62; $ # @ 60;%

NEXT

While Not Rs.eof

% $ # @ 62;

$ # @ 60; tr $ # @ 62;

$ # @ 60;% for each fld in @ 62;

$ # @ 60; td $ # @ 62; $ # @ 60;% = fld.value% $ # @ 62; $ # @ 60; / td $ # @ 62;

$ # @ 60;% Next

$ # @ 60; / tr $ # @ 62;

$ # @ 60;% rs.movenext

Wend% $ # @ 62;

$ # @ 60; / table $ # @ 62;

The following is a more efficient code, one output per line: $ # @ 60; table $ # @ 62;

$ # @ 60;%

For Each Fld in rs.fields

Response.write ("$ # @ 60; TH $ # @ 62;" & fld.name & "$ # @ 60; / th $ # @ 62;" & vbcrlf)

NEXT

While Not Rs.eof

Response.write ("$ # @ 60; TR $ # @ 62;")

For Each Fld in rs .fields% $ # @ 62;

Response.write ("$ # @ 60; TD $ # @ 62;" & fld.value & "$ # @ 60; / td $ # @ 62;" & vbcrlf)

NEXT

Response.write "$ # @ 60; / tr $ # @ 62;"

Wend

% $ # @ 62;

$ # @ 60; / table $ # @ 62;

This trick is very useful when Response Buffering is turned off. It is best to open Response Buffering so that you can see how the bulk response.wwrite improves program performance. Tips 13: Use $ # @ 60; Object $ # @ 62; Tag reference object If you need to reference objects except the code path (especially the server, Application range), use $ # @ 60 in the global.asa file Object runat = server id = ObjName $ # @ 62; tag to define them, not to use the Server.createObject method. Use the server.createObject method to create an object immediately, so if it is not used, the resource is ware. Use $ # @ 60; object id = ObjName $ # @ 62; tag can define object ObjName, but ObjName is actually created until its attribute or method is used for first time. Skill 14: Avoiding a series string in the loop Many people build a string in the loop, just like the following: s = "$ # @ 60; table $ # @ 62;" & vbcrlf

For Each Fld in rs.fields

S = S & "$ # @ 60; TH $ # @ 62;" & fld.name & "$ # @ 60; / tH $ # @ 62;"

NEXT

While Not Rs.eof

S = S & VBCRLF & "$ # @ 60; tr $ # @ 62;" For Each Fld In rs.fields

S = S & "$ # @ 60; TD $ # @ 62;" & fld.value & "$ # @ 60; / td $ # @ 62;"

NEXT

s = S & "$ # @ 60; / tr $ # @ 62;"

rs.movenext

Wend

S = S & VBCRLF & "$ # @ 60; / Table $ # @ 62;" & VBCRLF

Response.write S

There are several problems. The first is the repeated connection string consumes the secondary time, and the number of times the time and the quantity of the calculated field is also a square relationship. The following simple example is more clearly illustrated in this: s = ""

For i = ASC ("a") TO ASC ("Z")

S = S & CHR (i)

NEXT

At the first layer, the value of S is "a"; the Layer 2 cycle, VBScript To reassign strings, copy 2 characters ("AB") to S;, when the third layer is cycled, it needs to be Reallocate and copy 3 characters to s. When the NAC is cycled, you need to redistribute and copy n characters to s. That is the sum of 1 2 3 ... N, that is, n * (n 1) / 2 copies. In the above recordset example, if there are 100 records and 5 fields, the internal loop will perform 100 * 5 = 500 times, and the time to complete all copies and redistribution tasks will be close to 500 * 500 = 250,000. . This is also a copy of a proper size recordset. In this example, methods can be connected to response.write () or in-line scripts ($ # @ 60;% = fld.value% $ # @ 62;) by replacing strings. If Response Buffering is turned on (should also be opened), this will soon, because Response.Write is only added to the end of the buffer and does not need to be redistributed. If you use a JScript connection string, it is strongly recommended to use the " =" operator, that is, use S = "string" instead of s = s "string". Tips 15: Open the browser and agent buffer By default, the ASP prohibits the browser and the agent's buffer. If you do not update each time, you should open the browser and agent buffer, which will allow the browser and the agent to use the "buffer" copy data for this page for a while. Buffering can greatly reduce the server's data reprint, and improve user browsing performance. Which categories of dynamic pages are suitable for cache? Here are some examples:

转载请注明原文地址:https://www.9cbs.com/read-103889.html

New Post(0)