Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize this code

Tags:

performance

c#

it has a property: string Code and 10 other.

common codes is list of strings(string[] ) cars a list of cars(Car[]) filteredListOfCars is List.

for (int index = 0; index < cars.Length; index++)
{
    Car car = cars[index];
    if (commonCodes.Contains(car.Code))
    {
         filteredListOfCars.Add(car);
    }
}

Unfortunately this piece of methodexecutes too long.

I have about 50k records

How can I lower execution time??

like image 894
user278618 Avatar asked Mar 17 '10 16:03

user278618


1 Answers

The easiest optimization isto convert commonCodes from a string[] to a faster lookup structure such as a Dictionary<string,object> or a HashSet<string> if you are using .Net 3.5 or above. This will reduce the big O complexity of this loop and depending on the size of commonCodes should make this loop execute faster.

like image 159
JaredPar Avatar answered Nov 05 '22 17:11

JaredPar