This is a real performance problem.
public int FindPreviousFC(int framecode)
{
if (SetTable == null)
throw new NullReferenceException("Not loaded Log_Roadb.");
int previousFrameCode = 0;
for (int i = 0; i < SetTable.Rows.Count; i++)
{
if (framecode == Convert.ToInt32(SetTable.Rows[i][0]))
{
previousFrameCode = Convert.ToInt32(SetTable.Rows[i - 1][0]);
break;
}
}
return previousFrameCode;
}
If the data in the SetTable is ordered on
framecodethan you can use a binary search through the data structure to reduce the number of lookups.If there are not patterns in the data that you can exploit optimizing performance may become tricky. This assumes that you can’t export the data from
SetTableinto a structure where lookups are faster.If this Find method is being called frequently on the same set of data, then you may also want to consider creating an index structure (dictionary) to speed up subsequent lookups. This may mitigate the cost of iterating over the same data over and over.
Also, as an aside, don’t throw a
NullReferenceExceptionwhen you check theSetTableargument, throwArgumentNullExeceptioninstead. Null reference exceptions are thrown by the CLR when a reference variable that is null is dereferenced … it shouldn’t be thrown by your code.