In C# (.NET 3.5) I’ve filled a DataTable with rows from my database. In this DataTable there are about 100 to 200 DataRows. I have to loop through this DataTable to check if the data is correct and I use 27 check-methods. First I’ve tried to pass the DataTable to each method and loop it. In my second attempt I’ve looped the DataTable once and passed the DataRow to each method. Afterwards I’ve benchmarked these two methods and the first method was faster than the second?
Looping the DataTable 27 times took 13 seconds.
Looping the DataTable 1 time took 18 seconds.
So what’s the explanation for this? And what is really the fastest way to loop through a DataTable to check it’s data?
Note: the benchmark was started right before I started the check-methods to exclude connection speed to the database.
First method:
private void check()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
checkStamnr();
checkVoornaam();
checkGebDatum();
...
sw.Stop();
sw.Reset();
}
private void checkStamnr()
{
foreach (DataRow dr in dtIdentificatieRecords.Rows)
{
if (dr["STAMNRVOL"] == null || dr["STAMNRVOL"].GetType() == typeof(DBNull) || dr["STAMNRVOL"].equals(""))
{
DatabankFout df = new DatabankFout("Stamnummer is leeg.");
listDBFouten.Add(df);
}
}
}
Second method
private void check()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
foreach (DataRow dr in dtIdentificatieRecords.Rows)
{
checkStamnr(dr);
checkVoornaam(dr);
checkGebDatum(dr);
...
}
sw.Stop();
sw.Reset();
}
private void checkStamnr(DataRow dr)
{
if (dr["STAMNRVOL"] == null || dr["STAMNRVOL"].GetType() == typeof(DBNull) || dr["STAMNRVOL"].equals(""))
{
DatabankFout df = new DatabankFout("Stamnummer is leeg.");
listDBFouten.Add(df);
}
}
Class DatabankFout on request:
public class DatabankFout
{
public DatabankFout(string reden, bool rood)
{
this.reden = reden;
this.rood = rood;
}
public DatabankFout(string reden) : this(reden, false)
{
}
public string reden { get; set; }
public bool rood { get; set; }
}
I would say that a run-time difference of 25% in one run with so few items is not significant.
Run your test at least 10 times for 10000 items in the list.
If the first is still faster, I will have something to think about.
The main part of the time probably comes from creating the DatabankFout, which might be a database connection.