locked
It's too slow to use datatable.rows.add() RRS feed

  • Question

  • I want to add data to a datatable from SortedDictionary. I use datatable.rows.add(), but it's too slow when there is large amount of data in sortedDictionary. Is there another method?

     

    here is my code: (freqZi is a soredDictionary<char, int>)

     

    foreach ( KeyValuePair<char, int> kvp in freqZi)
         {
          DataRow rowToFind = dataTable.Rows.Find(kvp.Key);
          if (rowToFind == null)
          {
           DataRow rowToAdd = dataTable.NewRow();
           rowToAdd["zi"] = kvp.Key;
           rowToAdd["quantity"] = kvp.Value;
           dataTable.Rows.Add(rowToAdd);
          }

     

     

    Saturday, January 12, 2008 3:45 AM

Answers

  • Hello,

     

    How many elements have you in your Sorted Dictionary and in your DataTable ?

     

    I don't think that the SortedDictionary is guilty.

    For each element of your SortedDictionary, you do a find on your datable to verify whethet you have to add the element.

    I think that's the number of find which is guilty.

    I think you have to see with the structure of your datatable.

    Maybe you may gain some execution time by using access to our row by index instead by column name

    rowToAdd["zi"] = kvp.Key

     

    before the foreach :

    int izi = 0; /* if zi is the first column of the datable

    int iquantity = 1 ; /* if quantity is the second column of the datatable */

    rowToAdd[izi] = kvp.Key

    and idem for the second field

    See the link

    http://msdn2.microsoft.com/en-us/library/kwfe1t7z(VS.80).aspx

    If i remember, when you use index instead the name of a column, it'quicker because the prog has not to look at the datatable schema to get the index

     

    I hope this will help you

     

    Have a nice day

    Saturday, January 12, 2008 9:36 PM

All replies

  • Sort your data using queries. If you sort data in memory, you lose the benefit of using database index.

    Saturday, January 12, 2008 8:29 PM
  • I think the short answer is "no, there's not another method."

    Saturday, January 12, 2008 8:40 PM
  • Hello,

     

    How many elements have you in your Sorted Dictionary and in your DataTable ?

     

    I don't think that the SortedDictionary is guilty.

    For each element of your SortedDictionary, you do a find on your datable to verify whethet you have to add the element.

    I think that's the number of find which is guilty.

    I think you have to see with the structure of your datatable.

    Maybe you may gain some execution time by using access to our row by index instead by column name

    rowToAdd["zi"] = kvp.Key

     

    before the foreach :

    int izi = 0; /* if zi is the first column of the datable

    int iquantity = 1 ; /* if quantity is the second column of the datatable */

    rowToAdd[izi] = kvp.Key

    and idem for the second field

    See the link

    http://msdn2.microsoft.com/en-us/library/kwfe1t7z(VS.80).aspx

    If i remember, when you use index instead the name of a column, it'quicker because the prog has not to look at the datatable schema to get the index

     

    I hope this will help you

     

    Have a nice day

    Saturday, January 12, 2008 9:36 PM