Reputation: 12851
Assume we have a DataTable with 100 columns and 100 rows. We have a dictionary of values like this that contains 100 members:
Dictionary<int, string> dictionary = GetValues();
In above dictionary key is index of the Datatable and value of dictionary is target value that we want to update in Datatable.
What's the fastest way to update the Datatable? I use this way but it's slow :
foreach (var pair in dictionary)
{
timeTable.Rows[pair.Key].SetField(columnIndex, pair.Value);
}
Assuming we have to update all rows of 40 columns, it doesn't have good performance. I was thinking that Datatable is like a Matrix and it shouldn't take time at all when you want to update a cell on Matrix while you have indexes of the row and the column.
Upvotes: 0
Views: 2731
Reputation: 23813
DataTable is a very expensive structure that dos a lot behind the scenes (particularly because they are designed to handle anything).
If performance is critical consider:
Creating your own structure, such as a class encapsulating an array of strings arrays (e.g. a strings matrix, on which access will be very fast)
Udating your table in parallel (since you are never accessing the same row), knowing that DataTable is NOT thread safe for writing operations.
e.g.
// Probably not thread safe, after the MSDN documentation
Parallel.ForEach(Dictionary, pair =>
{
timeTable.Rows[pair.Key].SetField(columnIndex, pair.Value);
});
EDIT: This will not work in the general case (write operations even on distinct rows might affect global state of the DataTable, so I suggest that you go with your own structure if performance is critical, and if you don't need specific features of the DataTable)
Upvotes: 1