.net tuning record - Transfer Gu-style legend

The new company on a project and found a strange problem when doing performance testing, running the same process takes on a 48-core (HP580 G7 PC server) server 120 seconds, and a 4-core PC in on board as long as 90 seconds, with this question, the company hired the engineers of Microsoft to resolve this problem.

Trace debugging and optimizing After a day, the time-consuming to 70 seconds, which process involves several optimization .net object, indeed the effect is obvious. This article is the conclusion of a phased notes, but also very instructive.

 

A, DataView.ToTable performance issues () after

Engineers found the time to deal with this method is very slow in tracking code, occupies most of the time the entire logic of the code. This method is optimized effect is obvious.

Check MSDN, the official did not give DataView.ToTable () method tips on performance, but this method when you want to look at MSDN, found that in 2006, when foreigners have been back in the following piece of code to perform this in three ways method, found great differences in efficiency. ( The MSDN: DataView.ToTable () ).

I copied the code, create a new project, we can really reproduce the problem. The whole process is probably as follows:

BACKGROUND There is a 500,000 DataTable data, then the data word is transferred to this DataTable DataView, and then from this DataView by Totable () method, the data is transferred to another DataTable same architecture.

The first way to create a target DataTable objects, direct () method converts by toTable. Process took: 27.0504 seconds.

After creating the second goal DaTaTable way, it set the PrimaryKey. Data was then added to the target table by the following code.

                 foreach (DataRow dr in ds.Tables[0].Rows)

                {

                    DataRow[] drrepetido = dsRes.Tables[0].Select("valor=" + dr["Valor"]);

                    if (drrepetido.Length == 0)

                        dsRes.Tables[0].ImportRow(dr);

                }

Process took: 11.7624 seconds.

 

The third embodiment does not create the original target DataTable architecture, a new instance of the DataTable object, and then add a corresponding column, and then create a hash table, a target table write cycle, as follows:

                DataTable dt = new DataTable();

                dt.Columns.Add("valor", ds.Tables[0].Columns["valor"].DataType);

                Hashtable ht = new Hashtable();

                foreach (DataRow dr in ds.Tables[0].Rows)

                {

                    if (!ht.ContainsKey(dr[0]))

                    {

                        ht.Add(dr[0], null);

                        DataRow newRow = dt.NewRow();

                        newRow[0] = dr[0];

                        dt.Rows.Add(newRow);

                    }

                }

Process took: 0.2184 seconds.

 

Three ways contrast, found very scary. But Microsoft seems to have no reason given.

 

Two, StringBuilder () performance problems are not caused by file

 

After about before StringBuilder also read a lot of articles, the problems developers program and previous similar, do not set a more recommended when instantiating length value, resulting in continued appand in the loop body (), GC StringBuilder objects continue to be processed . Which consumes a lot of time.

About StringBuilder () an object, or a few words and then more of it. The default value is 16 Capacity maintenance.

Because the cost of StringBuilder to create larger objects, in the case of string concatenation fewer targets, excessive abuse can result in wasted StringBuilder performance rather than savings. Only a lot of unpredictable number or string operations, before considering to StringBuilder to achieve. String type "+" operatively connected, actually overloaded operators "+" call String.Concat operated, the compiler will optimize the operation of such a connection process, the compiler according to the number of its arguments passed, time allocation corresponding memory, and sequentially copy-in the corresponding string. StringBuilder in use, the best suitable capacity value specified otherwise due to insufficient capacity of the default memory allocation and frequent operation, is inappropriate implementation. Under normal circumstances, a simple string concatenation, and should be used in preference String.Concat String.Join other operations to complete the connection string, it will be possible to watch String.Concat boxing operation.

 

Third, enables me to bring performance issues

If StringBuilder.toString (). Trim () to determine whether there StringBuilder value, it may be StringBuilder.Lenth () instead.

Guess you like

Origin www.cnblogs.com/mlwork/p/11752745.html