This article is. the third article of Net parallel computing is welcome to everyone. To read this article, we need to have the basics of LINQ, because the parallel LINQ (PLinq) in fact, it is the parallel implementation of LINQ To Object. What is the parallel implementation of LINQ PLinq is actually the parallel implementation of Linq to Object. PLINQ will try To make full use of the system's processing. After the data source is split and then processed on multiple processes, this means that the running speed is significantly improved in most cases. PLINQ usually only needs to add AsParallel () query operations to the data source, the following example shows the List <int> TestDemo = Enumerable. range (1, 10000 ). toList (); Stopwatch w1 = Stopwatch. startNew (); var linqParallel = from c in TestDemo. asParallel () where c % 2 = 0 select c; int count = linqParallel. toList (). count; Console. writeLine ("concurrent Linq time consumption" + w1.ElapsedMilliseconds + "find the even number:" + count); you know how the above example is? In fact, PLinq is conservative by default. He will first analyze whether the overall structure and parallel query are secure. If the parallel query performance is improved and secure due to parallelism, the parallel query will be used. Otherwise, it will be executed in sequence. Of course, you can also directly specify to use parallel queries. Var linqParallel = from c in TestDemo. asParallel (). withExecutionMode (ParallelExecutionMode. forceParallelism) where c % 2 = 0 select c; Factors Affecting PLINQ query performance 1. the computing overhead of the overall work, first of all, everyone understands that there will be performance overhead in parallelism. Therefore, PLINQ queries must have enough computation to make up for such open-end operations, if the calculation workload is small, PLINQ is not suitable. Let's take a look at the example below to see if the running result is more time than PLINQ or more time than LINQ, in my computer, the time required for parallel LINQ is greater than that for non-parallel LINQ List <int> TestDemo = Enumerable. range (1, 1000000 ). toList (); Stopwatch w1 = Stopwatch. startNew (); var li NqParallel = from num in TestDemo. asParallel () where num/3 = 0 select num; Console. writeLine ("concurrent Linq time consumed" + w1.ElapsedMilliseconds); Stopwatch w2 = Stopwatch. startNew (); var linql = from num in TestDemo where num/3 = 0 select num; Console. writeLine ("non-parallel Linq time consumption" + w2.ElapsedMilliseconds); if we make the following changes, the performance advantages of PLINQ will immediately become apparent, this is because the SELECT statement has enough work to offset the performance overhead of parallelism. private static int MyTest (int a) {Thread. sl Eep (1000); Random r = new Random (1000); return r. next () ;}list <int> TestDemo = Enumerable. range (1,100 ). toList (); Stopwatch w1 = Stopwatch. startNew (); var linqParallel = from num in TestDemo. asParallel () where num/3 = 0 select MyTest (num); Console. writeLine ("concurrent Linq time consumed" + w1.ElapsedMilliseconds); Stopwatch w2 = Stopwatch. startNew (); var linql = from num in TestDemo where num/3 = 0 select MyTe St (num); Console. writeLine ("non-parallel Linq time consumed" + w2.ElapsedMilliseconds); Console. readLine (); 2. I really want to understand the logic kernel of the system. The Code is the same. You are on 4-core machines and 8-core machines, it must be because the 8-core running speed is faster because the total amount of work acceleration that can be shared among more parallel threads depends on the percentage of parallelism in the overall work of the query. However, do not assume that all queries run faster on an eight-core computer than on a quad-core computer. In PLINQ, we can specify the number of processors but cannot run 64 at most. in the following example, 1var linqParallel = from num in TestDemo. asParallel (). withDegreeOfParallelism (2) where num/3 = 0 select MyTest (num); 3. the query execution method will be discussed later here. 4. The number and type of operations. PLINQ provides the AsOrdered operator when the elements must be in the order of the source sequence. Sorting produces overhead, but this overhead is usually moderate. GroupBy and Join operations also produce overhead. If the elements in the source set are allowed to be processed in any order and the elements are passed to the next operator when they are ready