C # Performance Optimization

Source: Internet
Author: User

Continue with the previous article. The complete source code of this article is also in the previous article.
Enumeration array and common enumeration performance differences
Some people may know that. net has special optimization on arrays when processing enumeration. Therefore, when the enumeration set is an array, the performance will be better. For example, the following test code:
 
1 class C1 {
2
3 public void Do1 (){
4 int [] array = {1, 2, 3, 4 };
5 for (int I = 0; I <int. maxvalues/100; I ++ ){
6 DoIt1 (array );
7}
8}
9
10 private void DoIt1 <T> (IEnumerable <T> array ){
11 foreach (var item in array ){
12
13}
14}
15
16 public void Do2 (){
17 int [] array = {1, 2, 3, 4 };
18 for (int I = 0; I <int. maxvalues/100; I ++ ){
19 DoIt2 (array );
20}
21}
22
23 private void DoIt2 (int [] array ){
24 foreach (var item in array ){
25
26}
27}
28}
 
 
In the 23rd method, the compiler knows in advance that it is an array enumeration, so it will optimize the instruction. So what is the gap between such optimization? I need to test it.
 

The first is the result of Do1, and the second is the result of Do2. Obviously, the gap is still quite large. Why? From the perspective of the decompiled IL code, the first method uses the standard GetEnumerator mechanism and needs to create an instance and call the Current and MoveNext methods. What's more, the GetValue Implementation of Array is not fast enough. The second method uses the for mechanism, without the need to create an instance, and accumulating a judgment statement. The performance is of course high.
This is actually the code that has been taken into consideration in Linq to Object. For example:
 
Public static IEnumerable <TResult> Select <TSource, TResult> (this IEnumerable <TSource> source, Func <TSource, TResult> selector)
{
If (source = null)
{
Throw Error. ArgumentNull ("source ");
}
If (selector = null)
{
Throw Error. ArgumentNull ("selector ");
}
If (source is Enumerable. Iterator <TSource>)
{
Return (Enumerable. Iterator <TSource>) source). Select <TResult> (selector );
}
If (source is TSource [])
{
Return new Enumerable. WhereSelectArrayIterator <TSource, TResult> (TSource []) source, null, selector );
}
If (source is List <TSource>)
{
Return new Enumerable. WhereSelectListIterator <TSource, TResult> (List <TSource>) source, null, selector );
}
Return new Enumerable. WhereSelectEnumerableIterator <TSource, TResult> (source, null, selector );
}
 
 
 
Performance differences between creating classes and structures and performance differences between attributes and fields www.2cto.com
Next I will try to create a class instance and structure type. What is the difference in performance? Will. net's garbage collection be very powerful, basically not much different? Of course, I also tested the differences between access attributes and access fields.
 
1 class C1 {
2 public void Do1 (){
3 for (int I = 0; I <int. MaxValue/10; I ++ ){
4 var p = new PointClass () {X = 1, Y = 2 };
5}
6}
7 public void Do2 (){
8 for (int I = 0; I <int. MaxValue/10; I ++ ){
9 var p = new PointClass2 () {X = 1, Y = 2 };
10}
11}
12 public void Do3 (){
13 for (int I = 0; I <int. MaxValue/10; I ++ ){
14 var p = new Point () {X = 1, Y = 2 };
15}
16}
17 public void Do4 (){
18 for (int I = 0; I <int. MaxValue/10; I ++ ){
19 var p = new Point () {XP = 1, YP = 2 };
20}
21}
22}
23
24
25 class PointClass {
26 public int X {get; set ;}
27 public int Y {get; set ;}
28}
29
30 class PointClass2 {
31 public int X;
32 public int Y;
33}
34
35 struct Point {
36 public int X;
37 public int Y;
38
39 public int XP {get {return X;} set {X = value ;}}
40 public int YP {get {return Y;} set {Y = value ;}}
41}
 
 
The test results are as follows:

 
The experimental results show that in computing-intensive programs, structure creation is much more efficient than class creation. In addition, the performance of attribute and field access is basically the same.

 


From Writing life

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.