EDIT I'm limited to the .Net version 2.0 so I don't believe I can use the Task Parallel Library in this case.
I've got a dictionary of objects.
I need to iterate over all of them and perform an expensive, but embarrassingly parallelizable , calculation on each element.
Currently I'm using a single thread to iterate over the entire dictionary.
Approach 1
I've toyed with using a thread pool to split the calculations over multiple threads but that leads to the question of how to pass this to separate threads?
I currently convert the keys collection to an array and pass part of the array to separate threads so they can use the key to look up the value and perform the calculation.
Approach 2
Alternatively I could iterate over each key and dispatch a thread in a thread pool to each element.
The second approach is slower.
Is there a better alternative?
You can use the Task Parallel Library:
Parallel.ForEach(dictionary, keyValuePair => {...});
you can use this approach (.NET 4)
var elements = new ConcurrentDictionary<int, string>();
Parallel.ForEach(elements, (element) =>
{
// USE element the way you need it
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With