What is a parallel query?
Many aspects of a PLINQ query are similar to non-parallel LINQ to Objects queries. Like sequential LINQ queries, PLINQ queries perform operations on any in-memory IEnumerable or IEnumerable<T> data source and defer execution, i.e., they don't start executing until the query is enumerated. The main difference is that PLINQ tries to make the most of all processors on your system. The method is to partition the data source into fragments and then perform parallel queries on multiple processors for each fragment on a separate worker thread. In many cases, parallel execution means that queries run significantly faster.
With parallel execution, PLINQ can significantly improve performance (compared to older code for certain types of queries), often by simply adding AsParallel query operations to the data source. However, parallelism can introduce its own complexity, so not all query operations run faster in PLINQ. In fact, parallelism actually slows down some queries. Therefore, it is important to understand how issues such as sorting will affect parallel queries. For more information, see Understanding acceleration in PLINQ.
Simple example
For 10,000 pieces of data in the collection, the code is as follows:
Other features used by PLINQ
1. Using AsSequential, if you don't want to use parallel queries in the process, you can use this feature to restore to sequential queries.
2. Using AsOrdered, because PLINQ runs in parallel, the results may not be in order, which can be queried by adding the AsOrdered method.
3. Use WithDegreeOfParallelism, this property can set the number of CUPs in parallel on the computer.
Scene note
In many cases, queries can be parallelized, but the overhead of setting up parallel queries can outweigh the performance gains.If the query does not perform a large number of calculations, or if the data source is small, the PLINQ query may be slower than a sequential LINQ to Objects query。 You can use the Parallel Performance Analyzer in Visual Studio Team Server to compare the performance of various queries, find processing bottlenecks, and determine whether queries are running in parallel or sequentially.
Reference:The hyperlink login is visible.
|