Home >Backend Development >C#.Net Tutorial >C# performance optimization

C# performance optimization

伊谢尔伦
伊谢尔伦Original
2016-12-05 10:16:421272browse

Performance is an important indicator to consider the quality of a software product, and is equally important as the function of the product. When users choose a software product, they will basically test and compare the performance of similar products in person. As one of the important factors when choosing which software to buy.

What does software performance mean? 1. Downgrade memory consumption
In software development, memory consumption is generally a secondary consideration, because today’s computers generally have relatively large memory. In many cases, the means of performance optimization is space In exchange for time. However, this does not mean that we can waste memory unscrupulously. If you need to support use cases with large amounts of data, if the memory is exhausted, the operating system will frequently exchange internal and external memory. Resulting in a sharp drop in execution speed
2. Improve execution speed

loading speed.

Response speed for specific operations. Including clicks, keyboard input, scrolling, sorting and filtering, etc.



Principles of performance optimization

Understanding the requirements

Take the MultiRow product as an example. One of MultiRow's performance requirements is: "Smooth scrolling under millions of rows of data binding." This goal has always been considered during the development process of the entire MultiRow project .

Understand bottlenecks

According to experience, 99% of performance consumption is caused by 1% of the code. Therefore, most performance optimizations are targeted at this 1% bottleneck code. The specific implementation is divided into two steps. First, identify bottlenecks, and second, eliminate bottlenecks.

Don’t overdo it

First of all, you must realize that performance optimization itself has a cost. This cost is not only reflected in the amount of work spent on performance optimization. It also includes complex code written for performance optimization, additional maintenance costs, the introduction of new bugs, additional memory overhead, etc. A common problem is that some students who are new to software development will mechanically apply performance optimization techniques or design patterns to unnecessary points, bringing unnecessary complexity. Performance optimization often requires a trade-off between benefits and costs.


How to find performance bottlenecks

As mentioned in the previous section, the first step in performance optimization is to find performance bottlenecks. This section mainly introduces some practices for locating performance bottlenecks.

1. How to get the memory consumption

The following code can get the memory consumption of a certain operation.

// 在这里写一些可能消耗内存的代码,例如,如果想了解创建一个GcMultiRow软件需要多少内存可以执行以下代码
long start = GC.GetTotalMemory(true);
var gcMulitRow1 = new GcMultiRow();
GC.Collect();
// 确保所有内存都被GC回收
GC.WaitForFullGCComplete();
long end = GC.GetTotalMemory(true);
long useMemory = end - start;

2. How to get the time consumption

The following code can get the time consumption of a certain operation.

System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
for (int i = 0; i < 1000; i++)
{
     gcMultiRow1.Sort();
}
watch.Stop();
var useTime = (double)watch.ElapsedMilliseconds / 1000;

Here, an operation loop is executed 1000 times, and finally the consumed time is divided by 1000 to determine the final consumed time. The results can be more accurate and stable, and unexpected data can be eliminated.


3. Discover performance issues through CodeReview.
In many cases, performance issues can be discovered through CodeReview. Special attention should be paid to loops with large amounts of data. The logic within the loop should be executed as quickly as possible. 4.ANTS Performance Profiler
ANTS Profiler is a powerful performance testing software. It can help us find performance bottlenecks very well. Using this software to locate performance bottlenecks can achieve twice the result with half the effort. By skillfully using this tool, we can quickly and accurately locate code with performance problems. This tool is powerful, but it's not perfect. First of all, this is a paid software, and the department only has a few license numbers. Secondly, the working principle of this software is to add some hooks to IL to record time. Therefore, during analysis, the execution speed of the software will be slower than the actual operation, and the data obtained are therefore not 100% accurate. The data analyzed by the software should be used as a reference to help quickly locate the problem, but do not rely entirely on it, but also combine it with other techniques. to analyze program performance.

Methods and techniques for performance optimization

After locating the performance problem, there are many solutions. This chapter will introduce some performance optimization techniques and practices.

1. Optimize program structure

For program structure, you should consider it during design and evaluate whether it can meet performance requirements. If performance problems are discovered later, you need to consider adjusting the structure, which will cause a lot of overhead. Example:


1.1 GcMultiRowGcMultiRow needs to support 1 million rows of data. Assuming that each row has 10 columns, it will need 10 million cells, and each cell has many attributes. If no optimization is done, the memory overhead of a GcMultiRow software will be quite large when the amount of data is large. The solution adopted by GcMultiRow is to use a hash table to store row data. Only the rows changed by the user are placed in the hash table, and most of the rows that have not been changed are directly replaced by templates. This achieves the purpose of saving memory.


1.2 Spread for WPF/Silverlight (SSL)WPF的画法和Winform不同,是通过组合View元素的方法实现的。SSL同样支持百万级的数据量,但是又不能给每个单元格都分配一个View。所以SSL使用了VirtualizePanel来实现画法。思路是每一个View是一个Cell的展示模块。可以和Cell的数据模块分离。这样。只需要为显示出来的Cell创建View。当发生滚动时会有一部分Cell滚出屏幕,有一部分Cell滚入屏幕。这时,让滚出屏幕的Cell和View分离。然后再复用这部分View给新进入屏幕的Cell。如此循环。这样只需要几百个View就可以支持很多的Cell。


2. 缓存

缓存(Cache)是性能优化中最常用的优化手段.适用的情况是频繁的获取一些数据,而每次获取这些数据需要的时间比较长。这时,第一次获取的时候会用正常的方法,并且在获取之后把数据缓存下来。之后就使用缓存的数据。 如果使用了缓存的优化方法,需要特别注意缓存数据的同步,就是说,如果真实的数据发生了变化,应该及时的清除缓存数据,确保不会因为缓存而使用了错误的数据。 举例:


2.1 使用缓存的情况比较多。最简单的情况就是缓存到一个Field或临时变量里。      

for(int i = 0; i < gcMultiRow.RowCount; i++) 
{ 
    // Do something; 
}


以上代码一般情况下是没有问题的,但是,如果GcMultiRow的行数比较大。而RowCount属性的取值又比较慢的时候就需要使用缓存来做性能优化。 

int rowCount = gcMultiRow.RowCount;
for (int i = 0; i < rowCount; i++)
{
   // Do something;
}


2.2 使用对象池也是一个常见的缓存方案,比使用Field或临时变量稍微复杂一点。 例如,在MultiRow中,画边线,画背景,需要用到大量的Brush和Pen。这些GDI对象每次用之前要创建,用完后要销毁。创建和销毁的过程是比较慢的。GcMultiRow使用的方案是创建一个GDIPool。本质上是一些Dictionary,使用颜色做Key。所以只有第一次取的时候需要创建,以后就直接使用以前创建好的。以下是GDIPool的代码: 

public static class GDIPool 
{ 
    Dictionary<Color, Brush > _cacheBrush = new Dictionary<Color, Brush>(); 
    Dictionary<Color, Pen> _cachePen = new Dictionary<Color, Pen>(); 
    public static Pen GetPen(Color color) 
   { 
       Pen pen; 
       if_cachePen.TryGetValue(color, out pen)) 
       { 
           return pen; 
       } 
       pen = new Pen(color); 
      _cachePen.Add(color, pen); 
       return pen; 
   } 
}

2.3 懒构造 
有时候,有的对象创建需要花费较长时间。而这个对象可能并不是所有的场景下都需要使用。这时,使用赖构造的方法可以有效提高性能。 举例:对象A需要内部创建对象B。对象B的构造时间比较长。 一般做法:

public class A
{
   public B _b = new B();
}


一般做法下由于构造对象A的同时要构造对象B导致了A的构造速度也变慢了。优化做法:

public class A
{
   private B _b;
   public B BProperty
   {
       get
      {
         if(_b == null)
         {
             _b = new B();
         }
         return _b;
      }
   }
}


优化后,构造A的时候就不需要创建B对象,只有需要使用的时候才需要构造B对象。


2.4  优化算法 优化算法可以有效的提高特定操作的性能,使用一种算法时应该了解算法的适用情况,最好情况和最坏情况。 以GcMultiRow为例,最初MultiRow的排序算法使用了经典的快速排序算法。这看起来是没有问题的,但是,对于表格软件,用户经常的操作是对有序表进行排序,如顺序和倒序之间切换。而经典的快速排序算法的最差情况就是基本有序的情况。所以经典快速排序算法不适合MultiRow。最后通过改的排序算法解决了这个问题。改进的快速排序算法使用了3个中点来代替经典快排的一个中点的算法。每次交换都是从3个中点中选择一个。这样,乱序和基本有序的情况都不是这个算法的最坏情况,从而优化了性能。


2.5 了解Framework提供的数据结构 我们现在工作的.net framework平台,有很多现成的数据数据结构。我们应该了解这些数据结构,提升我们程序的性能: 
举例:
2.5.1 string 的加运算符 VS StringBuilder: 字符串的操作是我们经常遇到的基本操作之一。 我们经常会写这样的代码 string str = str1 + str2。当操作的字符串很少的时候,这样的操作没有问题。但是如果大量操作的时候(例如文本文件的Save/Load, Asp.net的Render),这样做就会带来严重的性能问题。这时,我们就应该用StringBuilder来代替string的加操作。

2.5.2 Dictionary VS List Dictionary和List是最常用的两种集合类。选择正确的集合类可以很大的提升程序的性能。为了做出正确的选择,我们应该对Dictionary和List的各种操作的性能比较了解。2.5.3TryGetValue 对于Dictionary的取值,比较直接的方法是如下代码:

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


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

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

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


2.5.4 为Dictionary选择合适的Key。 Dictionary的取值性能很大情况下取决于做Key的对象的Equals和GetHashCode两个方法的性能。如果可以的话使用Int做Key性能最好。如果是一个自定义的Class做Key的话,最好保证以下两点:1. 不同对象的GetHashCode重复率低。2. GetHashCode和Equals方法立即简单,效率高。

2.5.5 List的Sort和BinarySearch性能很好,如果能满足功能需求的话推荐直接使用,而不是自己重写。 

List<int> list = new List<int>{3, 10, 15};
list.BinarySearch(10); // 对于存在的值,结果是1
list.BinarySearch(8); // 对于不存在的值,会使用负数表示位置,如查找8时,结果是-2, 查找0结果是-1,查找100结果是-4.

复制代码

2.6 通过异步提升响应时间
2.6.1 多线程 
有些操作确实需要花费比较长的时间,如果用户的操作在这段时间卡死会带来很差的用户体验。有时候,使用多线程技术可以解决这个问题 举例: CalculatorEngine在构造的时候要初始化所有的Function。由于Function比较多,初始化时间会比较长。这是就用到了多线程技术,在工作线程中做Function的初始化工作,就不影响主线程快速响应用户的其他操作了。代码如下:

public CalcParser()
{
   if (_functions == null)
   {
       lock (_obtainFunctionLocker)
       {
           if (_functions == null)
           {
               System.Threading.ThreadPool.QueueUserWorkItem((s) =>
               {
                   if (_functions == null)
                   {
                       lock (_obtainFunctionLocker)
                       {
                           if (_functions == null)
                           {
                               _functions = EnsureFunctions();
                           }
                       }
                   }
               });
           }
       }
   }
}


这里比较慢的操作就是EnsureFunctions函数,是在另一个线程里执行的,不会影响主线程的响应。当然,使用多线程是一个比较有难度的方案,需要充分考虑跨线程访问和死锁的问题。

2.6.2 加延迟时间 
在GcMultiRow实现AutoFilter功能的时候使用了一个类似于延迟执行的方案来提升响应速度。AutoFilter的功能是用户在输入的过程中根据用户的输入更新筛选的结果。数据量大的时候一次筛选需要较长时间,会影响用户的连续输入。使用多线可能是个好的方案,但是使用多线程会增加程序的复杂度。MultiRow的解决方案是当接收到用户的键盘输入消息的时候,并不立即出发Filter,而是等待0.3秒。如果用户在连续输入,会在这0.3秒内再次收到键盘消息,就再等0.3秒。直到连续0.3秒内没有新的键盘消息时再触发Filter。保证了快速响应用户输入的目的。

2.6.3 Application.Idle事件 
在GcMultiRow的Designer里,经常要根据当前的状态刷新ToolBar上按钮的Disable/Enable状态。一次刷新需要较长的时间。如果用户连续输入会有卡顿的感觉,影响用户体验。GcMultiRow的优化方案是挂系统的Application.Idle事件。当系统空闲的时候,系统会触发这个事件。接到这个事件表示此时用户已经完成了连续的输入,这时就可以从容的刷新按钮的状态了。

2.6.4 Invalidate, BeginInvoke. PostEvent 平台本身也提供了一些异步方案。 
例如;在Winform下,触发一块区域重画的时候,一般不适用Refresh而是Invalidate,这样会触发异步的刷新。在触发之前可以多次Invalidate。BeginInvoke,PostMessage也都可以触发异步的行为。

2.7 了解平台特性 
如WPF的DP DP相对于CLR property来说是很慢的,包括Get和Set都很慢,这和一般质感上Get比较快Set比较慢不一样。如果一个DP需要被多次读取的话建议是CLR property做Cache。

2.8 进度条,提升用户体验 
有时候,以上提到的方案都没有办法快速响应用户操作,进度条,一直转圈圈的图片,提示性文字如"你的操作可能需要较长时间请耐心等待"。都可以提升用户体验。可以作为最后方案来考虑。


目前已有很多使用C#编写的开发工具,其中值得一提的是ComponentOne Studio Enterprise,这是一款专注于企业应用的.NET全功能控件套包,支持WinForms、WPF、UWP、ASP.NET MVC等多个平台,帮助在缩减成本的同时,提前交付丰富的桌面、Web和移动企业应用。

   


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn