. Net parallel computing

Source: Internet
Author: User

This article is the third article of. Net parallel computing. You are welcome To take a brick. To read this article, you need To have the basics of LINQ, because parallel LINQ (PLinq) is actually a parallel implementation of LINQ To Object.

  • What is parallel LINQ

PLinq is actually a parallel implementation of Linq to Object, and 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, this is simple. The following example shows this for everyone.

 

           List<> TestDemo = Enumerable.Range(,            Stopwatch w1 = linqParallel =  c  TestDemo.AsParallel()  c %  ==   count = + w1.ElapsedMilliseconds +  + count);

Do you know how the above example is executed? 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.

  linqParallel =  c  TestDemo.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)  c %  ==   c;
  • Factors Affecting PLINQ query performance

1. computing overhead of the overall work

First of all, we understand that there will be performance overhead in parallelization. Therefore, PLINQ queries must have enough computation to make up for such open-end operations. If the computation volume is small, PLINQ is not suitable for use; let's take a look at the example below to see if the running result is too much time in PLINQ or more time in 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 linqParallel = 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 be immediately apparent, because the SELECT statement has enough work to offset the performance overhead brought by parallelism.

    MyTest(=  Random(

 

            List<> TestDemo = Enumerable.Range(, = linqParallel =  num  TestDemo.AsParallel()  num /  ==   += linql =  num  TestDemo  num /  ==   +

2. Logical kernel of the system
I really want to understand this. The same Code is that on 4-core machines and 8-core machines, you must be able to share the work among more parallel threads because the eight-core is faster. The total amount of acceleration depends on the percentage of parallelism in the overall query work. 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:

var linqParallel = from num in TestDemo.AsParallel().WithDegreeOfParallelism(2) where num / 3 == 0 select MyTest(num);

3. query execution Method

I will talk about it later. I will not talk about it now.

4. Operation quantity and types

PLINQ provides the AsOrdered operator for elements that 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

 

 

 

 


 

Related Article

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.