惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
小众软件
小众软件
有赞技术团队
有赞技术团队
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AWS News Blog
AWS News Blog
C
Cisco Blogs
美团技术团队
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
W
WeLiveSecurity
D
DataBreaches.Net
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Vercel News
Vercel News
月光博客
月光博客
T
Tailwind CSS Blog
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
Recent Announcements
Recent Announcements

博客园 - EasyWriter

回顾工作这五六年来,该总结下得与失了 Log4Net 开发文档 面向对象的开发方法(Object Oriented,OO) 基于Lucene.net开源搜索 浅谈 数据库连接池〔翻译〕 转载“internet explorer 无法打开 interne 站点 已终止操作”错误的问题解决 海量数据的查询 海量数据库的查询优化及分页算法方案(转载) 把SQL SERVER里表里的数据导出成为insert into 脚本 静态(Static)方法 如何在子线程中操作窗体上的控件 .NET性能优化方面的总结 FCKeditor 2.2 + Asp.Net 设置 Asp.net直接保存文件到客户端 系统流程图简介 得到页面body区域内容的高度 面向对象设计原则 常用的正则表达式集锦 深入理解abstract class和interface
如何最大限度提高.NET的性能
EasyWriter · 2007-04-21 · via 博客园 - EasyWriter

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