. NET Application Performance Improvement tips

Source: Internet
Author: User
Tags join throw exception
Program | skill | performance use. NET to do development, performance is undoubtedly very important, how in the shortest possible time, to get the maximum performance, we are very concerned about each of the issues. Here, with MSDN tips, I'll do some finishing work.

All applications should be compliant.

Here's a list of some small suggestions that all applications can use to get high performance:

Throw the exception as early as possible: Throw Exception

Exception is very consumed, you can count your program inside how many exception, you read it will be greatly surprised, notice, don't forget, some systems own methods will also throw exception, for example, IO class. The following program you can try, it is a for loop, you run will find exception so terrible.


public static void Main (string[] args) {
int j = 0;
for (int i = 0; i < 10000; i++) {
try{
j = i;
throw new System.Exception ();
} catch {}
}
System.Console.Write (j);
Return
}
A few more multi-function functions

Try to avoid a function that accomplishes only a very small one or several functions, because it causes the system to call the function too frequently, causing performance degradation. We should combine some functions as possible, so that a function can complete several functions, so that the number of function calls is reduced to improve efficiency.

Work with a value type

When the data structure is simpler, use the struct of the value type instead of class, which will give you a performance boost. The following examples illustrate this point:

public struct foo{
Public foo (double arg) {this.y = arg;}
public double y;
}
public class bar{
Public bar (double arg) {this.y = arg;}
public double y;
}
Class class1{
static void Main (string[] args) {
System.Console.WriteLine ("Starting struct loop ...");
for (int i = 0; i < 50000000; i++)
{foo test = new Foo (3.14);}
System.Console.WriteLine ("struct loop complete.")
Starting object loop ... ");
for (int i = 0; i < 50000000; i++)
{Bar test2 = new bar (3.14);}
System.Console.WriteLine ("All done");
}
}
}

Running the program, you will find that the struct cycle is much faster than class. But use struct carefully, because it is not at any time is so fast.

Use AddRange instead of add in group operations

This is also an important technology, with AddRange allows us to join the things to join at once, rather than each time to add, so obviously can speed up. Almost all Windows control supports add and AddRange two ways.
The following lists the classes that support add and AddRange:

StringCollection, tracecollection, etc.
HttpWebRequest
UserControl
ColumnHeader


Get rid of what's not in the job

Remove unnecessary assemblies from your project because it lowers the performance of your program. Think about it, if you put all the assemblies are quoted, for the only one way, is not less than lost, so, the assembly are not used to take away.

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.