I'd like to write a Map-Extension Method for ParallelQuery without destroying the parallelism. Problem is, I have no idea how. I am using ParallelQuery because I'm confident the Multithreading will boost my performance, here's my code so far:
public static List<T2> Map<T, T2>(this ParallelQuery<T> source, Func<T, T2> func)
{
List<T2> result = new List<T2>();
foreach(T item in source)
{
result.Add(func(item));
}
return result;
}
As you can see, this kind of defeats the purpose of parallelism if I am correct. How would I do this correctly?
Thanks to Dykam for pointing out the the Select
-Method has exactly the behaviour I want. However, just for learning purposes I'd like to see how exactly this'd work, thanks!
Your question piqued my developer's heart, so I picked it up again and came up with this one:
public static IEnumerable<T2> Map<T,T2>(this ParallelQuery<T> source, Func<T,T2> func)
{
var list = new ConcurrentBag<T2>();
source.ForAll(s => list.Add(func(s)));
return list.ToList();
}
The ForAll
loops through the source
query in parallel and adds the result to a thread-safe collection.
I keep this one in mind when I need Select-like behaviour with some personal twist. It may be an efficient approach when function func
is a relatively heavy process.
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