Binary search for a list of objects?

I have List<Discount>, containing about 100,000 discount values. Here, Discount is a class, and I have List<Product>one that has about 200,000 entries.

I have for everyone that iterates through all the records List<Product>. For each iteration through the list of products, I take some values ​​from the product element and add it to the wrapper class. This wrapper class also contains a β€œDiscount” field, and for each product I have to go through all the discounts received.

The problem with repeating this list of products is time consuming. What is the best way to do this faster?

I was thinking about binary search, but is it difficult for me to implement it?

Any suggestion on how to do this?

+3
source share
3 answers

If you need a quick search for discount values, the Listdata structure may be incorrect. Consider using something like Dictionary , which is specifically designed for this purpose.

+4
source

Apart from the Heinzi proposal, I would also suggest revising the design / implementation, which forces you to load hundreds of thousands of objects into memory and cross-search against each other - perhaps you should implement this logic at the database level to get better performance.

+3
source

Do you find use Dictionaryinstead of a list? Searches will be much faster.

+1
source

Source: https://habr.com/ru/post/1773818/


All Articles