Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a Map-Method for ParallelQuery without defeating its purpose?

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!

like image 508
Dennis Röttger Avatar asked Nov 05 '22 03:11

Dennis Röttger


1 Answers

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.

like image 116
Gert Arnold Avatar answered Nov 09 '22 11:11

Gert Arnold