C # DataSet Performance Best Practices

Source: Internet
Author: User
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:}



    • According to the experiment, parallel computing is better than Select and loop filtering when the row selection of the DataTable is performed, while the performance of the rows is similar.


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!

  • 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.