优化 .NET的性能

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策略来提高性能

 

最新文章

  1. shell if 浮点数比较
  2. Log4j 简单应用
  3. 浅析Java.lang.ProcessBuilder类
  4. PL/SQL连接Oracle客户端步骤
  5. Linux system 函数的一些注意事项
  6. asp.net发送邮件
  7. grootJs 系统常用API接受
  8. jmeter制造安全证书
  9. JavaScript 要点(十六)RegExp 对象
  10. 大数据算法设计模式(2) - 左外链接(leftOuterJoin) spark实现
  11. HTTP/2之服务器推送(Server Push)最佳实践
  12. python来写打飞机
  13. MyEclipse中阿里JAVA代码规范插件(P3C)的安装及使用
  14. Keras实现LSTM
  15. python学习第二讲,pythonIDE介绍以及配置使用
  16. apache-tomcat 部分中文.html .jsp 连接 404问题
  17. Javascript - ExtJs - 事件
  18. Python单元测试unittest【转自https://www.cnblogs.com/feng0815/p/8045850.html】
  19. D. Bicolorings
  20. delphi shr和shl的作用

热门文章

  1. Python3基础 file open 打开txt文件并打印出全文
  2. 分页器的js实现代码 bootstrap Paginator.js
  3. Java-master(github)教材整理
  4. 获取CheckBox的值
  5. go 语言字典元素删除
  6. Qt5_QString_测试
  7. CodeForces - 369C - Valera and Elections
  8. The Front-End Checklist
  9. 3-19(晚) require_relative 和 require. === operator的解释。
  10. (转)nginx做转发时,带&#39;_&#39;的header内容丢失