Optimize Your compiled C # Program

Source: Internet
Author: User

When I first started learning a program, I often did not consider the efficiency of the program running. Most of the cases were to implement the function. As long as the function reaches what I wanted, I achieved this goal. When the coding implementation of most functions is not a big problem for us, we need to consider how to write programs more standardized and more efficient. For how to write efficiently, let's not talk about the overall architecture design. Just share some of the points in C #'s own programs that will affect performance. Suggested viewingC #Entry basics 01Tutorial.

1. Garbage Collection

The garbage collection cost of. NET Framework is high. Each time garbage collection is performed, the longest unused algorithm is called to find the objects that have not been used for the longest time and recycle the memory allocated to the objects. Such an algorithm consumes a lot of computing power on the computer. Therefore, do not create unnecessary objects in C # program coding.

1.1 avoid creating objects in a loop

An efficient way is to place the object creation outside the loop. However, if an object needs to be instantiated in each loop and added to the object list, you cannot create an object in the loop, if this object is created outside the object, the object added to the List multiple times will be the data of the last object of the loop.

1.2 create an object only when necessary

If an object is used only in some branch logics, place the operations for creating an object in the logical branch. Avoid creating all objects used by the function before the function.

2. String operation

String string operation, which we use multiple times. Therefore, inefficient processing of string operations will affect the execution efficiency of the program to some extent.

2.1 let's get to know about string

String is a constant class. If we use "+" to connect strings, a new space will be created to store the connected strings, when "+" is used in multiple cycles, the memory cycle is opened up. In this case, there should be a stringbuffer in stringbuilder. stringbuilder, and the connection operation will not allocate new string space every time.

2.2 The length of the string object is the fastest compared with 0, followed by string. Empty or empty string.

C # Put all the string constants declared in the Assembly into the reserved pool (intern pool) during compilation, and the same constants will not be allocated repeatedly.

3. Thread Synchronization

3.1 thread synchronization Granularity

C # When the thread synchronously locks an object through lock, we can lock a method or lock a piece of code in the method.

Generally, the synchronization range should be reduced to improve the system performance.

3.2 synchronization Policy

When using lock to lock objects, pay attention to the following two points:

First, avoid using the lock type. Locking the type object will affect all the locked objects in the process and will not be used by others. It may cause a non-Expires header error and the lock range is too large.

Second, avoid locking this. This in the program indicates the current object. Locking the modified object instance will affect other methods used in this example.

Third, we recommend that you find the minimum range to be locked and instantiate an object to synchronize the object.
4. CLR, C # intermediate language

CLR is the compiled assembly of C # language. It is an intermediate language designed by Microsoft.

4.1 valuetype and referencetype

The value type is allocated from the stack, and the reference type is allocated from the stack. When the value type is used as a method parameter, the parameter value is assigned by default. Therefore, transferring data using a reference type requires a high ratio of data transmission efficiency.

4.2 avoid packing and unpacking

To put it simply, binning converts the value type to the reference type, and unboxing converts the reference type to the value type.

5. Exception Handling

In the 5.1 program, you must run the exception where the exception is thrown. Use try... Catch... Module package, which has nothing to do with performance, but is necessary for program troubleshooting.

5.2 The program throws an exception and captures the exception, which consumes a large amount of operations. When programming, the program logic should be improved to avoid throwing exceptions.

6. Use of reflection

It delays static binding during compilation to dynamic binding during runtime. Reflection brings a lot of design flexibility, but dynamic binding has a greater impact on performance than static binding.

1 type. invokemember

2 contructorinfo. Invoke

3 activator. createinstance (type)

4 activator. createinstance (assemblyname, typename)

5 assembly. createinstance (typename)

The fastest performance is 3, the slowest is 4

Suggestion:

1. If possible, avoid using reflection and dynamic binding.

2. Use the interface call method to convert dynamic binding to early binding

3 activator. createinstance (type)

4. Use the typeof operator instead of GetType.

7. General rules

Recommended

8. Performance Optimization policies

8.1 storage devices are getting cheaper and cheaper. In practice, most of the optimization work is trying to change the time with space.

8.2 performance bottleneck diagnosis: use professional tools to diagnose code performance, or use stopwatch of C # To calculate the program code execution time.

8.3 performance optimization practices: optimizes the system structure, uses cache, delays execution, optimization algorithms, and asynchronous/multithreading Programming

Learn more about IT skillsProgramming Language tutorialKnowledge, please log on to e Liang Shi Yiyou network to learn the tutorial knowledge.

Optimize Your compiled C # Program

Related Article

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.