C # Performance Tuning details
1. Use ItemArray to realize batch assignment of DataRow
When assigning values to all fields of a DataRow, using field names for column-wise assignment is less efficient. You should use bulk field assignments whenever possible. You can use the ItemArray or Rows.Add method:
/DS is a DataSet (DataSet) object datatable dt = ds. Tables[0];D atarow row = dt. NewRow (); row. ItemArray = new object[] {value1, value2, ..., Valuen};//DS is a DataSet (DataSet) object datatable dt = ds. Tables[0];d T. Rows.Add (value1, value2, ..., valuen);//You should avoid doing a large number of consecutive single-column assignments, as follows: DataTable dt = ds. Tables[0];D atarow row = dt. NewRow (); row["col1"] = value1;row["col2"] = value2;...row["Coln"] = Valuen;
2. Parallel computation of rational use of DataTable
The built-in parallel computing in the DataTable can take advantage of every CPU of the computer to optimize efficiency.
Ienumerable<datarow> findrows ()//Find all entries with a quantity less than 0 { DataTable dt = itemdatatable; ... Return dt. Select ("quantity<0"); No parallel calculations are used}ienumerable<datarow> findrows ()//Find all entries of less than 0 { DataTable dt = itemdatatable; ... int index = dt. Columns.indexof ("Quantity"); Return dt. AsEnumerable (). AsParallel (). Where (dr = (decimal) Dr[index] < 0); Using Parallel Computing:}
3, using ImportRow to achieve the same structure DataTable merger
Using the merge method makes it easy to merge the DataTable, but the merge is very inefficient, and the example is as follows:
datatable[] Srctables = ...; foreach (DataTable src in srctables) {dest. Merge (SRC);}
ImportRow can also implement a DataTable merge, which is much higher performance than merge. The code examples are as follows:
datatable[] Srctables = ...; foreach (DataTable src in srctables) { foreach (DataRow row in Src. Rows) { dest. ImportRow (row);} }
4. To be Continued
The above is the content of the C # DataSet performance Best practices, please follow topic.alibabacloud.com (www.php.cn) for more information!