Of course, the site performance optimization is multifaceted, here to talk about these days to obtain:
1, the habit of writing code;
Again the complex logic, is also from the simplest start. In the process of writing code, many bad specifications will affect the performance of the site;
Here are some code habits to sort out:
1 Comparison of strings with string. Empty instead of ""
2 in the traversal process, the first definition of a good count variable, and then traversal, which will reduce each traversal of the allocation of memory space:
Copy CodeThe code is as follows:
int i;
for (i=0; i<100;i++)
{
Codeing
}
3 Similarly, use Stringbuilder.append () instead of [CSharp] string + = "ABC" [/csharp];
4 processing logic in the traversal loop rather than invoking other forms of the method, which can have performance loss when invoked
5 when accumulating/reducing, use a+=1,a-=1 instead of a=a+1; this slows down the number of application memory
6) Multi-use set operation
A: If the element type in the set is fixed, you can use their corresponding collection classes, such as group, generics, etc., so that you can avoid boxing, unboxing operation;
B: If the number of elements in the collection is fixed and the type is consistent, array storage is used;
C: If the operation of the collection is primarily a lookup aspect, you can use the hashtable,dictionaty<tyey,tvalue>
7 Use Server.Transfer for page redirection
Benefits: Performance is much better than Response.Redirect, and has the benefit of hiding URLs, you can avoid the client page redirection;
Disadvantage: If the user with a refresh or backward will cause an unexpected situation, so please use caution
8 Reduce the use of server controls
9 Reasonable (appropriate time) to use the cache
10 reduce the size of cookies
... ...
2. Database
1 The database was opened at the latest and closed at the earliest.
2 optimize the database connection configuration, for large web site because there is a large number of data in the database, so you can increase its database connection pool
Max Pool size=512; (Defaults to 100)
3 Optimizing SQL statements and using stored procedures
Note: Try to avoid SQL statements such as "SELECT * from", try not to use subqueries in query statements, and use indexes as much as possible;
4) using DataReader
In DataReader, we often use the dr["field name" to find the form, but this writing is the most performance-consuming;
Generally, using a lookup based on "serial number" is more efficient than a "named" Lookup. Among them, can be divided into 4 categories:
Writing (1) using DataReader Index + search based on "serial number", such as: dr[1]. ToString (),
The writing (2) uses the DataReader index + "named" lookup, such as: dr["LastName". ToString (), which is the most bad performance.
Writing (3) using the method beginning with Get + search based on "serial number", such as: Dr. GetString (1), writing
The method of writing (4) using the GetSQL beginning with the search based on "serial number", such as: Dr. GetSqlString (1),
The notation (5) uses a lookup based on 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 it changes when we insert a new field. The role of GetOrdinal () is to let ourselves serialize the corresponding fields.
A, first defines the serial number int classid,classnameid;
b, then the new serial number is given by the getordinal () method;
C, through the getsqlsring (serial number) method can find the corresponding fields.
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 ();
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.