AsParellel extionsion will process your query in parallel over multiple cores


AsParellel extionsion will process your query in parallel over multiple cores.

The use of the AsParallel extension is that eays that you tend to use the AsParallel extension for every query you write. So I did a litle search and found the following:

  • Overhead:

There is a litle overhead. So it's possible that a standard query is faster than a parallel query if the amount of data is small enough.

  • Ordered list:

Not always the list is returned in the order you will expect. This is caused by the fact that the data is splitted up etc. To avoid this you can use the AsOrdered extension but this also has some overhead, so again you have to measure the amount of data against this overhead.

The AsParallel extension has on itself some specific extensions available starting with with:

  • WithCancelation:
  • WithDegreeOfParallelism
  • WithExecutionMode
  • WithMergeOptions

WithCancelation enables you to cancel the query.

WithDegreeOfParallelism sets the maximum of use of cores to use for this query. Default it's using all the cores available.

WithExecutionMode, in some cases linq may decide that your query is better dealt with sequentially. You can force the query to run parallel with by using this extension and specifying the ParallelExecutionMode enumeration as ForceParallelism.

WithMergeOptions, this will specify how the query will buffer the output. If you will want to return the results as soon as they starting to become available you can specify ParallelMergeOptions.NotBuffered. The other option wich is faster in total is to wait for all results and return that: ParallelMergeOptions.FullyBuffered

Exception handling

Another nice thing of this extension is the exception handling. Normally exception handling on different threads is not that easy but Plinq catches all the exceptio in a aggregate class:

    catch (AggregateException e)
        foreach (var ex in e.InnerExceptions)
            if (ex is IndexOutOfRangeException)
                Console.WriteLine("The data source is corrupt. Query stopped.");




Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!