157 recommendations for writing high-quality code to improve C # programs--recommendation 84: Using PLINQ

Source: Internet
Author: User

Recommendation 84: Use PLINQ

The most basic function of LINQ is to iterate over the collection and manipulate the elements on that basis. Careful scrutiny will reveal that parallel programming is simply a special preparation for this type of application. As a result, Microsoft has extended a class parallelenumerable specifically for LINQ (which is also in namespace System.Linq), which provides extension methods that allow LINQ to support parallel computations, which is called PLINQ.

The traditional LINQ calculation is single-threaded, and PLINQ is concurrent and multithreaded, and we can see this difference in the following example:

Static voidMain (string[] args) {List<int> intlist =Newlist<int> () {0,1,2,3,4,5,6,7,8,9 }; varquery = fromPinchIntlistSelectp; Console.WriteLine ("the following is the LINQ sequential output:"); foreach(intIteminchquery) {Console.WriteLine (item.      ToString ()); } Console.WriteLine ("The following is the PLINQ parallel output:"); varQueryparallel = fromPinchIntlist.asparallel ()Selectp; foreach(intIteminchqueryparallel) {Console.WriteLine (item.      ToString ()); }  } 

The output of LINQ is printed in the order in which it is indexed in Intlist. The output of PLINQ is disorganized.

There is another way to handle parallel output, which is to Queryparallel ForAll:

Queryparallel.forall (item) ={      Console.WriteLine (item). ToString ());  

But this approach poses a problem, and if you want to sort the results after the parallel output, ForAll ignores the asordered request for the query. As shown below:

var  from inch Select p;   = ={      Console.WriteLine (item. ToString ());  

The Asordered method can regroup the parallel computed queue to preserve the order. However, in the ForAll method, the output it accomplishes is still unordered. If you want to maintain the requirements of the Asordered method, we should always use the first parallel approach, namely:

var  from inch Select p;   foreach (int in queryparallel)  {      Console.WriteLine (item. ToString ());  

Sorting after parallel queries will sacrifice some performance. Some extension methods sort the elements by default, including: Order BY, OrderByDescending, ThenBy, and ThenByDescending. In practical use, it is important to note the differences between the various ways so that the program runs according to our assumptions.

There are other methods of querying, such as take. If we encode this:

foreach (int item in Queryparallel.take (5))
{
Console.WriteLine (item. ToString ());
}
In a sequential query, the first 5 elements are returned. In PLINQ, however, 5 unordered elements are selected.

It is recommended that you use PLINQ instead of LINQ when manipulating element items in a collection. Keep in mind, however, that not all parallel queries are faster than sequential queries, and sequential queries can be faster when performing certain methods on a collection, such as method ElementAt. In development, we should carefully identify the needs in order to find the best solutions.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs--recommendation 84: Using PLINQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.