You won’t have to worry about performance with as few trees/items you will have. If you have 10 trees, with 90 items each, that makes 900 calls to contains, which has to run over each item in the tree (90 items) for a total of 81K comparisons in your code. Your machine is measured in GHz of speed, so running this should take such a small fraction of a second that you wouldn’t even notice. The big-O notation of this algorithm is O(m*n^2) where m is number of trees, and n is the size of the trees. This isn’t too bad for small m and n (which you have relatively).
If this is for a website, and speed does become an issue, there are approaches you can take, but it’d probably be easier/better to look at caching this output rather than making the computation faster.
If you do need to make it faster, there are things you can do. One potential optimization would be instead of iterating over UnionedItemList
calling .Contains
on t
, make UnionedItemList
a hash table (dictionary) and iterate over t.items
setting the appropriate value in UnionedItemList
. In theory this is then just O(m*n), calling only 900 comparisons in your case, but the downside is added complexity, and at this scale the overhead of using a hash table may be worse than just doing it the way you’re doing it.
1
solved Comparing a number of lists and output a comparison table using c#/Html