Asp.net: website performance optimization

Source: Internet
Author: User
Tags website performance

Of course, there are many aspects of website performance optimization. Here we will first talk about the results of these days:
1. the habit of writing code;
The complicated logic also begins with the simplest one. In the process of writing code, many bad specifications will affect the website performance;
The following are some of the Code habits:
1) For string comparison, use string. Empty instead ""
2) during the traversal process, the counting variable is defined first and then traversed, which will reduce the memory space allocated once each traversal: Copy codeThe Code is as follows: int I;
For (I = 0; I <100; I ++)
{
// Codeing
}

3) Similarly, use StringBuilder. Append () to replace [csharp] string + = "ABC" [/csharp];
4) process the logic in the traversal loop instead of calling other methods. This will cause performance loss during calls.
5) When accumulating/decreasing, use A + = 1, A-= 1, instead of A = A + 1. This slows down the number of memory requests.
6) multi-purpose set operation
A: If the element types in the collection are fixed, you can use their respective collection classes, such as arrays and generics, to avoid packing and unpacking;
B: If the number of elements in the set is fixed and the types are consistent, array storage is used;
C: If you want to perform search operations on a set, you can use HashTable, Dictionaty <TYey, TValue>.
7) Use Server. Transfer for page redirection
Benefits: the performance is much better than response. redirect, and the benefits of hiding URLs can avoid page redirection on the client;
Disadvantage: If you use refresh or rewind, this will lead to unexpected situations, so please use it with caution.
8) reduce the use of server controls
9) use the cache properly (when appropriate)
10) Reduce the cookie size
......
2. Databases
1) The database is opened and closed at the latest;
2) Optimize the database connection configuration. You can increase the database connection pool for large websites because there is a large amount of data in the database.
Max Pool Size = 512; (default value: 100)
3) Optimize SQL statements and use stored procedures
Note: Avoid SQL statements such as "select * from" as much as possible; do not use subqueries in query statements as much as possible; and use indexes whenever possible;
4) use DataReader
In DataReader, we often use the dr ["field name"] search form, but this method is the most performance-consuming;
Generally, search by serial number is more efficient than search by name. There are four types:
Method (1) Use DataReader index + search based on "Serial Number", for example, dr [1]. ToString (),
Writing Method (2) using DataReader index + search based on "name", such as: dr ["LastName"]. ToString (), which is the worst performing Writing Method
Method (3) method starting with Get + search based on "Serial Number", for example: dr. GetString (1)
(4) Methods Starting with GetSql + search based on "Serial Number", for example: dr. GetSqlString (1 ),
(5) search using the serial number + GetOrdinal () method.
What is the GetOrdinal () function method?
We write the corresponding field from the database to the serial number we are looking for, but sometimes when inserting a new field, it will change. GetOrdinal () is used to serialize the corresponding fields.
A. first define the serial number int classid and classnameid;
B. Then, use the GetOrdinal () method to assign the new serial number;
C. Use the GetSqlSring (serial number) method to find the corresponding field.
The sample code is as follows:Copy codeThe Code is as follows: SqlConnection con = new SqlConnection (connString );
String strcmd = "select top 1 classid, classname where cms_class ";
SqlCommand cmd = new SqlCommand (strcmd, con );
Con. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
Int numberId, numberName;
NumberId = Convert. ToInt32 (dr. GetOrdinal ("classid "));
NumberName = Convert. ToInt32 (dr. GetOrdinal ("classname "));
Con. Close ();
If (dr. Read ())
{
String result = dr. GetSqlString (numberId) + "|" + dr. GetSqlString (numberName );
}
Dr. Close ();

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.