最佳化 .NET的效能

來源:互聯網
上載者:User

最佳化 .NET的效能 
   
轉自:http://www.cnblogs.com/Donets/archive/2007/04/05/701726.html

  1)避免使用ArrayList。
   因為任何對象添加到ArrayList都要封箱為System.Object類型,從ArrayList取出資料時,要拆箱回實際的類型。建議使用自訂的集合類型代替ArrayList。.net 2.0提供了一個新的類型,叫泛型,這是一個強型別,使用泛型集合就可以避免了封箱和拆箱的發生,提高了效能。
  
  2)使用HashTale代替其他字典集合類型(如StringDictionary,NameValueCollection,HybridCollection),存放少量資料的時候可以使用HashTable.
  
  3)為字串容器聲明常量,不要直接把字元封裝在雙引號" "裡面。 
   

//避免 
   // 
   MyObject obj = new MyObject(); 
   obj.Status = "ACTIVE"; 
   
   //推薦 
   const string C_STATUS = "ACTIVE"; 
   MyObject obj = new MyObject(); 
   obj.Status = C_STATUS; 

   
  4) 不要用UpperCase,Lowercase轉換字串進行比較,用String.Compare代替,它可以忽略大小寫進行比較.
  
   例: 

   
   const string C_VALUE = "COMPARE"; 
   if (String.Compare(sVariable, C_VALUE, true) == 0) 
   { 
   Console.Write("SAME"); 
   } 
   

   
  5) 用StringBuilder代替使用字串串連符 “+”,.
  
   //避免 

   String sXML = "<parent>"; 
   sXML += "<child>"; 
   sXML += "Data"; 
   sXML += "</child>"; 
   sXML += "</parent>"; 

   
   //推薦 

   StringBuilder sbXML = new StringBuilder(); 
   sbXML.Append("<parent>"); 
   sbXML.Append("<child>"); 
   sbXML.Append("Data"); 
   sbXML.Append("</child>"); 
   sbXML.Append("</parent>"); 

   
  6) If you are only reading from the XML object, avoid using XMLDocumentt, instead use XPathDocument, which is readonly and so improves performance.
  如果只是從XML對象讀取資料,用唯讀XPathDocument代替XMLDocument,可以提高效能

 

   //避免 
   XmlDocument xmld = new XmlDocument(); 
   xmld.LoadXml(sXML); 
   txtName.Text = xmld.SelectSingleNode("/packet/child").InnerText; 
   
  . 
   
   //推薦 
   XPathDocument xmldContext = new XPathDocument(new StringReader(oContext.Value)); 
   XPathNavigator xnav = xmldContext.CreateNavigator(); 
   XPathNodeIterator xpNodeIter = xnav.Select("packet/child"); 
   iCount = xpNodeIter.Count; 
   xpNodeIter = xnav.SelectDescendants(XPathNodeType.Element, false); 
   while(xpNodeIter.MoveNext()) 
   { 
   sCurrValues += xpNodeIter.Current.Value+"~"; 
   } 

 

7) 避免在迴圈體裡聲明變數,應該在迴圈體外聲明變數,在迴圈體裡初始化。 
   

   //避免 
   for(int i=0; i<10; i++) 
   { 
   SomeClass objSC = new SomeClass(); 
   . 
   . 
   . 
   
   } 
   
   //推薦 
   SomeClass objSC = null; 
   for(int i=0; i<10; i++) 
   { 
   objSC = new SomeClass(); 
   
   . 
   . 
   . 
   } 
   

  8) 捕獲指定的異常,不要使用通用的System.Exception. 
   

//避免 
   try 
   { 
   <some logic> 
   } 
   catch(Exception exc) 
   { 
   <Error handling> 
   } 
   
   //推薦 
   try 
   { 
   <some logic> 
   } 
   catch(System.NullReferenceException exc) 
   { 
   <Error handling> 
   } 
   catch(System.ArgumentOutOfRangeException exc) 
   { 
   <Error handling> 
   } 
   catch(System.InvalidCastException exc) 
   { 
   <Error handling> 
   } 
   

   
  9) 使用Try...catch...finally時, 要在finally裡釋放佔用的資源如串連,檔案流等
  不然在Catch到錯誤後佔用的資源不能釋放。 
   

   try 
   { 
    
   } 
   catch 
   {} 
   finally 
   { 
   conntion.close() 
   } 

  10) 避免使用遞迴調用和嵌套迴圈,使用他們會嚴重影響效能,在不得不用的時候才使用。
  
  11) 使用適當的Caching策略來提高效能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.