Performance: searching and updating

G

Guest

Hi. I have profiled my code and discovered where the bottlenecks are:

1. To find unique records I have been using DataTable.Select() but this
appears to be quite slow. What is the fastest method of searching keyed data
for a row with a particular key value?

2. At one point I need to do cell by cell updates to my data. For example if
I have an array of values for a particular column I do this:

for( int rowIndex = 0; rowIndex < myTable.Rows.Count; ++rowIndex )
myTable.Rows[ rowIndex ][ "MyColumn" ] = myArray[ rowIndex ];

Again this is quite expensive. What can I do to speed up this kind of
update? Turn off constraints? Enable some kind of batch update?

Thanks

kh
 
M

Miha Markic [MVP C#]

kh said:
Hi. I have profiled my code and discovered where the bottlenecks are:

1. To find unique records I have been using DataTable.Select() but this
appears to be quite slow. What is the fastest method of searching keyed
data
for a row with a particular key value?

DataTable.Rows.Find should do just fine.
2. At one point I need to do cell by cell updates to my data. For example
if
I have an array of values for a particular column I do this:

for( int rowIndex = 0; rowIndex < myTable.Rows.Count; ++rowIndex )
myTable.Rows[ rowIndex ][ "MyColumn" ] = myArray[ rowIndex ];

Again this is quite expensive. What can I do to speed up this kind of
update? Turn off constraints? Enable some kind of batch update?

DataTable.BeginLoadData, EndLoadData.
Perhaps you should also block redrawing of bound control if there is any.
 
G

Guest

Thanks Miha. I'll give them a go

kh


Miha Markic said:
kh said:
Hi. I have profiled my code and discovered where the bottlenecks are:

1. To find unique records I have been using DataTable.Select() but this
appears to be quite slow. What is the fastest method of searching keyed
data
for a row with a particular key value?

DataTable.Rows.Find should do just fine.
2. At one point I need to do cell by cell updates to my data. For example
if
I have an array of values for a particular column I do this:

for( int rowIndex = 0; rowIndex < myTable.Rows.Count; ++rowIndex )
myTable.Rows[ rowIndex ][ "MyColumn" ] = myArray[ rowIndex ];

Again this is quite expensive. What can I do to speed up this kind of
update? Turn off constraints? Enable some kind of batch update?

DataTable.BeginLoadData, EndLoadData.
Perhaps you should also block redrawing of bound control if there is any.
 
A

Anubhav Mishra

Use .Find which is the fastest, but the keys should be the primary key in
the table,

Select is slower.
Actually it depends on the number of rows that you have, as the DataTable is
not strongly types its not the best option, depending on the size of the
application you can choose to go for strongly typed collection. either using
hashtable or hashlist [Have to wite the same]

Anubhav Mishra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top