Several ways to achieve performance goals

Source: Internet
Author: User
Tags exception handling resource sleep
Performance for the first time here to write a blog, I hope not to be teased!
I. Adjusting the program code
1. Avoid reading the same data multiple times
Do not include code that is not executed repeatedly in a circular statement, such as:
while (Dr.read ())
{
if (dr.item["LastName"]==request.params["LastName"])
return true;
Else
return false;
}
In the loop above, for each record returned by the DataReader to the request.params["LastName" to evaluate once, if Dr Returns 100 records, we will request.params["LastName"] Evaluated 100 times, and these 100 times 99 times are redundant, the following code relative efficiency is higher
String strlastname=request.params["LastName"];
while (Dr.read ())
{
if (dr.item["LastName"]==strlastname)
return true;
Else
return false;
}

Summary: Reading values from any type of collection is slow, and when we need to access it more than once, you can save a copy in a local variable first

2. Use Response.Write () on the connection string;
The connection string is a very resource-less operation, we want to connect a 10-byte character with a 30-byte character, the code must allocate a 40-byte memory area, copy all the bytes to this area, and then point to the area at the position where the pointer is being adjusted, The Response.Write () method provides efficient buffering and connectivity services when releasing the original resource, so the following code is generally
Response.Write ("headline=");
Response.Write ("Strheadline");
Response.Write (", descreption=");
Response.Write (dr.item["descreption"]);
than the following code
Response.Write ("headline=") &_
Strheadline&_
", descreption=" &_
dr.item["Descreption"];
High efficiency
Summary: Try to connect strings using Response.Write () and StringBuilder
3. Avoid frequent calls to COM components into managed code.
4. Avoid changing the dimensions of the array
Changing the dimensions of an array dynamically in a program is actually creating a new array with the requested size, copying the contents of the original array to the inside, deleting the original array, and replacing the old array address with the new array, which takes a lot of effort.
5. Do not rely too much on exception checking in your code
Exception handling is a very resource-intensive operation, and you should use exception handling sparingly for common code to detect situations
such as try
{
int a,b;
a=100/b;
}
Catch
{
b=0;
}
It's better to use the following code efficiently
if (b==0)
{
Error ();
}
High efficiency
Summary: The use of exception handling mechanisms should be done sparingly
6. Early binding should be implemented to prevent late binding by adding the O ption= "Strict" option in the @page instruction of a Web page to prevent data from being used when data is converted
Two. Adjust the use of ASP.net
1. Disable Debug and Trace mode
These two patterns can significantly degrade performance when you deploy a product-level application
2. Pre-loading data in the Application object
It is possible to improve the performance of a program by loading a few frequently used and relatively static data into the application through a one-time read data application object once built nine keep in memory, until the end of the program, for the WEBL is the server shutdown, but the
A few mbyte of data exist in the application is not a good choice
3. Use Page.IsPostBack to avoid unnecessary processing
Initialization code is not necessary to run when the actual form is submitted, and the ViewState mechanism saves the initialization elements of any of our forms
4. Proper use of server controls
Adding runat=server to the control tag increases the burden on the Web server. Do not add this property to the markup when the server handles the control's events, unless it is necessary to handle the control's properties
5. Use of ViewState when necessary
The more controls the viewstate contains, the greater the server burden, the use of ViewState can also cause trouble, such as a Label control that displays error messages, and we want to not repeat the message on every page to clear a control's ViewState property to set its Enableviewsate property to False.
6. Disable session on pages that do not use sessions
7. Cache data and page output as much as possible
Cache can greatly improve performance, we can write outputcache instructions in the page
8. Avoid unnecessary data round-trip to the server
Validation controls can be used to reduce unnecessary round-trip data transfer
The more hasty behind, the wife urged to sleep, do not sleep estimated to ..., hehe.





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.