How to optimize iterating over two lists

How to optimize iterating over two lists



I have two external api's that return each a series of dates and values. List1 returns a TotalCount and List2 returns a CompletedCount. For each date NotCompletedCount = TotalCount - CompletedCount.

The current implementation is a ForEach List1, with a nested ForEach List2 and an if that checks if = and then perfoms the caluculation when true.

These lists are then combined to on to create a SourceDataPointList as source for a Charts\ColumnChart

If the series of dates contains 50 dates this will result in 50x50 = 2500 iterations with only 50 matches to operate on

Does anybody know how to do this in a more efficient way?

Hi Paul,

If you have no control over the external apis, right now, I see two options (maybe there's more):

  1. Everytime you find a match in the 2nd list, delete that item from the list. This will reduce the number of comparisons to half.
  2. Sort both lists (which has a cost but is less than NxN on average) and then you will only have N comparisons instead of NxN.

To large lists, I would say the 2nd is better.

Maybe somebody has a better idea...




hmm, interesting :)

some ideas out of my head:

1. Use a hashtable(s), so you only have to loop though 1?

2. instead of looping, do a list-filter (not sure how that action is implemented)

and yes, sorting and remove "older" dates should be cleaning up the list for sure.

so in the last records you have a small list to loop...


Thanks, the hashtable is quite easy: