C # Performance Optimization

Source: Internet
Author: User

Continue with the previous article.Source codeAlso in the previous articleArticle.

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 testCode:

 1       Class  C1 {  2   3           Public   Void  Do1 (){  4               Int [] Array = { 1 , 2 ,3 , 4  };  5               For ( Int I = 0 ; I < Int . Maxvalue/ 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 . Maxvalue/ 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

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 computing-intensiveProgramMedium,Structure creation is still much more efficient than the class. In addition, the access performance of attributes and fields is basically the same.

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.