How to optimize this code

has the property: line Code and 10 others.

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

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

Unfortunately, this method fragment is too long.

I have about 50 thousand records

How to reduce lead time

+6
performance c #
source share
6 answers

Jared correctly pointed out that you can optimize this with a HashSet , but I would also like to point out that the whole method is not needed, losing memory for the output list and making the code less clear.

You can write the whole method as:

 var commonCodesLookup = new HashSet<int>(commonCodes); var filteredCars = cars.Where(c => commonCodesLookup.Contains(c.Code)); 

The execution of the filteredCars filtering operation will be delayed, so if the consumer wants only the first 10 elements, that is, using filteredCars.Take(10) , then it is not necessary to build the entire list (or any list at all).

+16
source share

The easiest optimization is to convert commonCodes from string[] to a faster search structure such as Dictionary<string,object> or HashSet<string> if you are using .Net 3.5 or higher. This will reduce the great complexity of this loop, and depending on the size of commonCodes, this loop will run faster.

+20
source share

To do what you want, I would use the Linq ToLookup method to create an ILookup instead of using a dictionary. ToLookup was specifically created for this type of script. This is basically an indexed group search. You want to group your cars with Code .

 var carCodeLookup = cars.ToLookup(car => car.Code); 

Creating a carCodeLookup will be slow, but you can use it to quickly find Code based cars. To get a list of cars that are in the list of regular codes, you can quickly browse.

 var filteredCarsQuery = commonCodes.SelectMany(code => carCodeLookup[code]); 

This assumes that your car list does not change very often, and your common codes are dynamic between requests.

+1
source share

you can use linq join command like

 var filteredListOfCars = cars.Join(commonCodes, c => c.Code, cC => cC, (car, code) => car).ToArray(); 
0
source share

Here's an alternative to linq options (which are also good ideas): if you are trying to quickly filter, I would suggest using the built-in types. You can create a DataTable in which there are two fields, the identifier of the car in your array and the code (you can add 10 more things if they are also important). Then you can create a DataView around it and use the filter property. It uses some really fast indexing internally (B-trees, which I believe), so you probably won’t be able to outperform it manually unless you are a whiz algorithm, which if you were, you wouldn’t ask here . It depends on what you do and how important performance is.

0
source share

It looks like you are really checking to see if the “code” is common, not a car. You can look at a sample of the weight of flies, where cars share common instances of Code objects. Then the code object can have the IsCommon property and the Value property. Then you can do something to update the used code objects whenever the list of common codes changes. Now, when you do your filtering, you only need to check each car code IsCommon property

0
source share

All Articles