• C#提升性能tips


    1,for vs foreach

    2,structure vs class

    3,property vs dicrect

    4, array vs list

    5,Avoid Rethrowing Exceptions

    6, Refactor your algorithms to rely on hash table

    constant time search

    The absolutely killer
    performance collections are hash tables
    , implemented through HashSet<T>
    and Dictionary<K,V>.  

     7, Throw Fewer Exceptions

    8, Make Chunky Calls

    A chunky call is a function call that performs several tasks, such as a method that initializes several fields of an object. 

    9, Keep IO Buffer Size Between 4KB and 8KB

    10,Be on the Lookout for Asynchronous IO Opportunities

    11, 避免不必要的对象创建 
    由于垃圾回收的代价较高,所以C#程序开发要遵循的一个基本原则就是避免不必要的对象创建。以下列举一些常见的情形。 
    11.1 避免循环创建对象 ★ 
    如果对象并不会随每次循环而改变状态,那么在循环中反复创建对象将带来性能损耗。高效的做法是将对象提到循环外面创建。 
    11.2 在需要逻辑分支中创建对象 
    如果对象只在某些逻辑分支中才被用到,那么应只在该逻辑分支中创建对象。 
    11.3 使用常量避免创建对象 
    程序中不应出现如 new Decimal(0) 之类的代码,这会导致小对象频繁创建及回收,正确的做法是使用Decimal.Zero常量。我们有设计自己的类时,也可以学习这个设计手法,应用到类似的场景中。 

     12,Dictionary VS List Dictionary和List是最常用的两种集合类。选择正确的集合类可以很大的提升程序的性能。为了做出正确的选择,我们应该对Dictionary和List的各种操作的性能比较了解。 下表中粗略的列出了两种数据结构的性能比较。 

    操作

    List

    Dictionary

    索引

    Find(Contains)

    Add

    Insert

    Remove

    13,TryGetValue 对于Dictionary的取值,比较直接的方法是如下代码: 

    if(_dic.ContainKey("Key")
    {
    return _dic["Key"];
    }

    当需要大量取值的时候,这样的取法会带来性能问题。优化方法如下:

    object value;
    if(_dic.TryGetValue("Key", out value))
    {
    return value;
    }

    使用TryGetValue可以比先Contain再取值提高一倍的性能。

    14,如果只是从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+ ",";  
    }
    15,避免使用递归调用和嵌套循环,使用他们会严重影响性能,在不得不用的时候才使用。
    16 , sealed

    http://codebetter.com/patricksmacchia/2009/04/19/micro-optimization-tips-to-increase-performance/

    https://msdn.microsoft.com/en-us/library/ms973839.aspx

    https://blogs.msdn.microsoft.com/ricom/2006/03/09/performance-quiz-9-ilistlttgt-list-and-array-speed/

    https://msdn.microsoft.com/zh-cn/library/ms973858.aspx

    https://blogs.msdn.microsoft.com/vancem/

    https://zhuanlan.zhihu.com/p/28471848

  • 相关阅读:
    C++中的字符串可以这样换行写
    2020年最受欢迎的 10 门编程语言
    10 本最适合初学者和高级程序员的Python书籍
    手把手教你用 Python + Flask 搭建个人博客
    CentOS VS Ubuntu,谁才是更好的 Linux 版本?
    Python丨为什么你学不好设计模式?
    突破C++瓶颈,在此一举!
    从零上手 GDB 调试,看这个教程就够了~
    搞机器学习需要数学基础吗?
    用 Python 读写 Excel 表格,就是这么的简单粗暴且乏味
  • 原文地址:https://www.cnblogs.com/kevin1988/p/8733695.html
Copyright © 2020-2023  润新知