Hi everyone,
i've got a problem using Generic.List. I'm currently writing a program which has to deal with a lot
of data stored in a Generic.List. I often need to search through the list to find out if a new item
is already stored in the list. This is no problem beacause of the Contains-Method. The problem
is this method is very slow and the processing has to be very fast. I've come up with writing an
anonymous function which does the seacrhing for me whcih is way faster. looking like this:
col = getNewColor();
if (!colors.Exists(delegate(Color c) { return c == col; }))
{
colors.Add(col);
}//
This is also not that fast, concerning what I need for my programm. Is there any way
to speed-up a search through a list?
I appreciate any help.
Greetings LeRoi
i've got a problem using Generic.List. I'm currently writing a program which has to deal with a lot
of data stored in a Generic.List. I often need to search through the list to find out if a new item
is already stored in the list. This is no problem beacause of the Contains-Method. The problem
is this method is very slow and the processing has to be very fast. I've come up with writing an
anonymous function which does the seacrhing for me whcih is way faster. looking like this:
col = getNewColor();
if (!colors.Exists(delegate(Color c) { return c == col; }))
{
colors.Add(col);
}//
This is also not that fast, concerning what I need for my programm. Is there any way
to speed-up a search through a list?
I appreciate any help.
Greetings LeRoi
Posted Dec 12, 2007, 5:10 PM
RobertPosted Nov 25, 2007, 9:58 AM
Greets LeRoi
AlanPosted Nov 24, 2007, 6:31 PM
Don't laugh but have you tried a simple traversal of the list using a for (not foreach) loop which is often the fastest method of all as there's virtually no overhead:
col = getNewColor();
bool exists = false;
for(int i = 0; i < colors.Count; i++)
{
if (colors[i] == col)
{
exists = true;
break;
}
}
if (!exists)
{
colors.Add(col);
}