C # Performance Optimization practices

Source: Internet
Author: User
Tags execution screen detach

Performance mainly refers to two aspects: memory consumption and execution speed. Performance optimization in short, is to make it run faster without affecting the correctness of the system's operation, and it takes less time to complete a particular function.

This article takes. NET platform, describes the practice of C # Performance optimization For example, MultiRow.

Performance optimization Principles

Understand the needs

One performance requirement for multirow is: "Millions of rows of data are bound to smooth scrolling." "The development of the entire MultiRow project has been taking this goal into account.

· Understanding bottlenecks

99% of the performance consumption is due to 1% of the code. Most of the performance tuning is done for this 1% bottleneck code. The concrete implementation also divides into two steps: "Discovers the bottleneck" and "eliminates the bottleneck".

· Avoid excessive

Performance optimization itself is cost-optimized. This cost is not only reflected in the amount of work done to optimize performance, but also involves writing complex code for performance optimization, resulting in additional maintenance costs, such as introducing new bugs, additional memory overhead, and so on. Performance optimization often requires trade-offs between benefits and costs.

How to discover performance bottlenecks

The first step in performance optimization is to discover performance bottlenecks, and here are some practices for locating performance bottlenecks.

· How to get memory consumption

The following code can get the memory consumption of an operation.

Long start = GC. Gettotalmemory (true);
Write code here that needs to be tested for memory consumption, for example, to create a gcmultirow
var gcMulitRow1 = new Gcmultirow ();
Gc. Collect ();
Ensure that all memory is collected GC by GC
. WaitForFullGCComplete ();
Long end = GC. Gettotalmemory (true);
Long usememory = End-start;

How to get time consumption

The following code can get an operation time consumption.

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;

To achieve more stable time consumption, an operation loop is performed 1000 times, taking the average of time consumed to exclude unstable data.

· ANTS Performance Profiler

ANTS Performance Profiler is a powerful performance testing software. With this tool, we can quickly and accurately navigate to code with performance problems. This is a billing software, will add some hooks in the IL to record time, so in the analysis, the software execution speed will be slower than the actual operation, the data obtained is not absolutely accurate, but also the combination of other techniques to analyze the performance of the program.

· Codereview

Codereview is the last resort for discovering performance problems. Codereview should focus as much attention on the performance bottlenecks of the product as possible to ensure that part of the logic executes as fast as it can.

Methods and techniques of performance optimization

After positioning the performance problem, there are many ways to solve it. Here are some techniques and practices for performance optimization.

· Optimization program Structure

You should consider whether the product structure can meet performance requirements at design time. If performance problems are discovered later, adjusting the structure can have a very large overhead.

For example:

Gcmultirow to support 1 million rows of data. Assuming that there are 10 columns per row, you need 10 million cells, and a lot of properties on each cell. If you do not do any optimization, large amount of data, the memory cost of a gcmultirow software will be quite large. Gcmultirow uses a hash table to store row data: Only the rows that are changed by the user are placed in the Hashtable, and most of the rows that are not changed are directly replaced with templates. This achieves the goal of saving memory.

Unlike the descriptive and WinForm platforms of the WPF platform and the Silverlight platform, it is accomplished by combining visual elements. Spreadgrid for WPF products also support the amount of millions data, but you cannot assign a view to each cell. So Spreadgrid uses Virtualizingpanel to achieve the drawing. The idea is that each visual is a cell display module that can be detached from the cell's data module, so that you only need to create visual for the displayed cell. When scrolling occurs, a portion of the cell rolls out of the screen, and a portion of the cell rolls into the screen. At this point, let the cell that rolls out of the screen detach from visual, and then reuse this part of the cell that is visual to the new entry screen. With this loop, you need only hundreds of visual to support a lot of the cell.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.