Optimizing ASP Program Skills Four

Source: Internet
Author: User
Tags arrays expression functions advantage
Program | tips | Optimization This article will introduce you to four optimize the ASP application techniques.





  1. Cache frequently used data in application or session objects

ASP's application and session objects provide a convenient container for staging data in memory. You can assign data to application and session objects, which are kept in memory before HTTP calls. Session data is stored separately for each user, while application data is shared among all users.

When will the data be loaded into the application or session? Typically, data is loaded when the application or session is started. To load data during the application or session startup process, add the appropriate code to either Application_OnStart () or Session_OnStart (). These functions should be in Global.asa, and if not, we can add them by hand. You can also mount the data the first time it is needed. To do this, add some code (or write a reusable script function) to the ASP page to check whether the data exists, and if not, load the data. This is a traditional technique for high performance, called "Lazy computing," where you don't calculate a value until you know you need one. Examples of procedures are as follows:

<%
Function getemploymentstatuslist
Dim D
d = Application (? Employmentstatuslist?)
If d =?? Then
' Fetchemploymentstatuslist function (not shown)
' Fetches data from DB, returns an Array
D = fetchemploymentstatuslist ()
Application (? Employmentstatuslist?) = d
End If
Getemploymentstatuslist = d
End Function
%>

We can write similar functions for each block of data that we need.

  2. Encapsulate code in a COM object

If you have a lot of VBScript or JScript code in your Web application, you can often move the code to a compiled COM object, which can improve performance. Encapsulating code in a COM object also has a number of advantages beyond improving performance:

(1) COM object is advantageous to separate the presentation logic from the business logic;

(2) COM object can ensure the reuse of code;

(3) code written in VB, VC + + or Visual J + + is easier to debug than ASP.

COM objects also have drawbacks, including the long initialization time and the need for different programming techniques. It is important to note that when a small amount of ASP code is encapsulated into a COM object, performance may not be improved and performance degradation will occur. This is often the case that the overhead of creating and invoking COM objects exceeds the overhead of compiling the code. Therefore, we need to experiment over and over again to determine what kind of combination of ASP script and COM object code can produce the best performance.

  3. Handling embedded scripts and Response.Write statements

The <% = expression%> of VBScript syntax is to write the value of "expression" into the ASP output stream. If the response buffer is not enabled, executing each of these statements will write the data to the browser over the network with many small packets. In this way, slow execution and a small amount of scripting and HTML interspersed will result in a switch between the scripting engine and the HTML, which can greatly degrade performance. Therefore, we can use the following technique, which is to use the Response.Write call instead of a tightly bound inline expression.

For example, in the following example, each field in each row has a write operation for the response stream, and there are multiple switches between VBScript and HTML on each line:

<table>
<% for each fld in Rs. Fields%>
<th><% = fld. Name%></th>
<%
Next
While not Rs. Eof
%>
<tr>
<% for each
FLD in Rs. Fields%>
<td><% = fld. Value%></td>
<% Next
</tr>
<% Rs. MoveNext Wend%>
</table>

To make the above code more efficient, we can take advantage of the following code, each line of the code has a write operation on the response stream, and all the code is contained within a VBScript program:

<%
Response.Write (<TABLE>?)
For each fld in Rs. Fields
Response.Write <th>? && fld. Name && </th>? && vbCrLf)
Next
While not Rs. Eof
Response.Write (<TR>?)
For each fld in Rs. Fields%>
Response.Write <td>? && fld. Value && </td>? && vbCrLf)
Next
Response.Write (</TR>?)
Wend
Response.Write (</TABLE>?)
%>

This technique is particularly effective when response buffering is disabled. It is a good idea to enable response buffering and see if batch Response.Write helps improve performance.

  4. Avoid re-determining the dimensions of an array

When applying arrays, we should try to avoid using ReDim arrays. In terms of performance, if the computer's physical memory size is limited, it is a good idea to set the initial dimensions of the array to its most unfavourable condition, or set the dimension to its best case, and then redefine the dimension as needed.

Taking advantage of the above techniques in developing an ASP's Web application will make your Web program perform well.







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.