Επεξεργασία

Κοινή χρήση μέσω


.NET Performance Tips

The term performance generally refers to the execution speed of a program. You can sometimes increase execution speed by following certain basic rules in your source code. In some programs, it is important to examine code closely and use profilers to make sure that it is running as fast as possible. In other programs, you do not have to perform such optimization because the code is running acceptably fast as it is written. This article lists some common areas where performance can suffer and tips for improving it as well as links to additional performance topics. For more information about planning and measuring for performance, see Performance

Boxing and Unboxing

It is best to avoid using value types in situations where they must be boxed a high number of times, for example in non-generic collections classes such as System.Collections.ArrayList. You can avoid boxing of value types by using generic collections such as System.Collections.Generic.List<T>. Boxing and unboxing are computationally expensive processes. When a value type is boxed, an entirely new object must be created. This can take up to 20 times longer than a simple reference assignment. When unboxing, the casting process can take four times as long as an assignment. For more information, see Boxing and Unboxing.

Strings

When you concatenate a large number of string variables, for example in a tight loop, use System.Text.StringBuilder instead of the C# + operator or the Visual Basic Concatenation Operators. For more information, see How to concatenate multiple strings and Concatenation Operators in Visual Basic.

Finalizers

Empty finalizers should not be used. When a class contains a finalizer, an entry is created in the Finalize queue. When the finalizer is called, the garbage collector is invoked to process the queue. If the finalizer is empty, this simply results in a loss of performance. For more information, see Finalizers and Object Lifetime: How Objects Are Created and Destroyed.

Other Resources

See also