With a good coding style, the written code is not only clear, but also enhances the robustness of the program. At the same time, it complies with some coding specifications, this will greatly improve the program performance. The following code specifications can improve the program performance for your reference.
(1) Avoid creating objects in the loop body: Some objects do not change the state every time in the loop body, therefore, creating an object in the loop body will lead to performance loss caused by the creation of the object and the collection of the object. The creation of the object code should be referred to the external body of the loop.
Nonstandard code:
1 For (INT I = 0; I <length; I ++)
2 {
3 person _ person = new person ();
4 ......
5}
6
Standard Code:
1 person _ person = new person ();
2 For (INT I = 0; I <length; I ++)
3 {
4 ......
5}
6
(2) Avoid packing and unpacking: converting a value type object to a reference type will result in the packing operation. Converting the reference type back to the value type will lead to the unpacking operation. A typical use is arraylist. The item in arraylist is of the object type. If the value type, such as int type, is used in actual application, operations as an araylist element can cause performance problems described above.
Nonstandard code:
1 arraylist list = new arraylist ();
2 For (INT I = 0; I <100; I ++)
3 {
4 list. Add (I );
5}
6 ......
7 For (Int J = 0; j <list. Count; j ++)
8 {
9 int value = (INT) list [J];
10 ......
11}
Standard Code
1 List <int> List = new list <int> ();
2 For (INT I = 0; I <100; I ++)
3 {
4 list. Add (I );
5}
6 ......
7 For (Int J = 0; j <list. Count; j ++)
8 {
9 int value = list [J];
10 ......
11}
12
(3) When connecting multiple strings (the connection operation is greater than 5), use stringbuilder and use the + operator to create a new string.
Nonstandard code:
1 string STR = "string ";
2 For (INT I = 0; I <100; I ++)
3 {
4 STR + = I. tostring ();
5}
6
Standard Code:
1 stringbuilder strbd = new stringbuilder ("string ");
2 For (INT I = 0; I <100; I ++)
3 {
4 strbd. append (I );
5}
6 string STR = strbd. tostring ();
7
(4) is and as keywords: a typical application scenario is to determine whether an object is of a certain type. If yes, it is explicitly converted to this type. In fact, this operation causes the CLR to verify the object type twice and has a certain impact on the performance.
Nonstandard code:
1 If (DS is dataset)
2 {
3 dataset _ DS = (Dataset) ds;
4 ......
5}
6
Standard Code:
1 dataset _ DS = ds as dataset;
2 If (_ DS! = NULL)
3 {
4 ......
5}
6
Standard code is not only easy to read and maintain, but also can enhance the performance of the program. The above examples are performance problems caused by nonstandard Code. These performance problems are sometimes not very prominent, but we should always be aware of this when writing code. If there are too many performance problems, the overall program will be greatly affected.
Author: Dang Jian
Source: http://www.cnblogs.com/dangjian/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.
Also follow my independent blog-http://www.dang-jian.com.