C # parallel and buy-in platforms build parallel collections and PLINQ

Source: Internet
Author: User

Co-purchase platform construction

The emergence of parallel algorithms, along with the creation of parallel collections, and thread-safe collections, Microsoft is thoughtful, did not forget LINQ, also introduced a parallel version of LINQ, Plinq-parallel LINQ.

One, parallel collections-thread-safe collections

Parallel computing uses multiple threads to compute at the same time, so to control access to resources for each thread, let's take a look at the usual list<t> collection, perform in parallel computations, create a new console application, add a penumerable class ( Of course, you also write directly to the main method inside the test, suggested to write separately, write the following method:

Copy Code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Collections.Concurrent;

Namespace ThreadPool
{
public class Penumerable
{
public static void Listwithparallel ()
{
list<int> list = new list<int> ();
Parallel.For (0, 10000, item =
{
List. ADD (item);
});
Console.WriteLine ("List ' s count is {0}", list. Count ());
}
}
}

Copy Code

Click F5 to run and get the following results:

See the results shown in 5851, but we are looping 10,000 times Ah! What's wrong with the result? This is because list<t> is a non-thread-safe collection, meaning that all threads can modify his value.

Let's take a look at the parallel collection-thread-safe collection, in the System.Collections.Concurrent namespace, first look at the concurrentbag<t> generic collection, which uses a similar list<t>, Let's start by writing a test:

Copy Code

public static void Concurrentbagwithpallel ()
{
concurrentbag<int> list = new concurrentbag<int> ();
Parallel.For (0, 10000, item =
{
List. ADD (item);
});
Console.WriteLine ("Concurrentbag ' s count is {0}", list. Count ());
}

Copy Code

Execute two methods at the same time, with the following results:

As you can see, the result of the Concurrentbag collection is correct. Let's change the code to see how the data in Concurrentbag is stored, and modify the code as follows:

Copy Code

public static void Concurrentbagwithpallel ()
{
concurrentbag<int> list = new concurrentbag<int> ();
Parallel.For (0, 10000, item =
{
List. ADD (item);
});
Console.WriteLine ("Concurrentbag ' s count is {0}", list. Count ());
int n = 0;
foreach (int i in list)
{
if (n > 10)
Break
n++;
Console.WriteLine ("item[{0}] = {1}", n,i);
}
Console.WriteLine ("Concurrentbag ' s max item is {0}", list. Max ());

  }

Copy Code

Let's look at the results of the operation:

As you can see, the data in the concurrentbag is not arranged in order, and the order is chaotic and random. We also use the LINQ methods of Max, first, last, and so on. It is similar to the usage of enumerable, you can refer to Microsoft's MSDN to understand its specific usage.

There are a lot of thread-safe collections, similar to those we normally use, such as dictionary concurrentdictionary, and Concurrentstack,concurrentqueue.

Second, the usage and performance of Parallel LINQ

1, AsParallel

As you learned about parallel for and foreach, let's take a look at the parallel versions of LINQ today. To test, we add a custom class with the following code:

public class Custom
{
public string Name {get; set;}
public int Age {get; set;}
public string Address {get; set;}
}

Write the following test code:

Copy Code

public static void Testplinq ()
{
Stopwatch SW = new Stopwatch ();
list<custom> customs = new List<custom> ();
for (int i = 0; i < 2000000; i++)
{
Customs. ADD (New Custom () {Name = "Jack", age = +, Address = "NewYork"});
Customs. ADD (New Custom () {Name = "jime", age = +, Address = "China"});
Customs. ADD (New Custom () {Name = "Tina", age = $, Address = "Shanghai"});
Customs. ADD (New Custom () {Name = "Luo", age = +, Address = "Beijing"});
Customs. ADD (New Custom () {Name = "Wang", age = $, Address = "Guangdong"});
Customs. ADD (New Custom () {Name = "Feng", age = +, Address = "Yunnan"});
}

     sw.Start();     var result = customs.Where<Custom>(c => c.Age > 26).ToList();     sw.Stop();     Console.WriteLine("Linq time is {0}.",sw.ElapsedMilliseconds);     sw.Restart();     sw.Start();     var result2 = customs.AsParallel().Where<Custom>(c => c.Age > 26).ToList();     sw.Stop();     Console.WriteLine("Parallel Linq time is {0}.", sw.ElapsedMilliseconds);  }

Copy Code

In fact, add a AsParallel () method, below to see the results of the operation:

The time difference is one times, but sometimes does not differ so much, depends on the system current resource utilization. You can test it a bit more.

In fact, AsParallel () This method can be applied with any set, including the List<t> collection, thus improving query speed and system performance.

2. GroupBy method

In the project, we often have to do the data processing, such as group statistics, we know in LINQ can also be implemented, today to learn about the new ToLookup method, write a test method, the code is as follows:

Copy Code

public static void Orderbytest ()
{
Stopwatch Stopwatch = new Stopwatch ();
list<custom> customs = new List<custom> ();
for (int i = 0; i < 2000000; i++)
{
Customs. ADD (New Custom () {Name = "Jack", age = +, Address = "NewYork"});
Customs. ADD (New Custom () {Name = "jime", age = +, Address = "China"});
Customs. ADD (New Custom () {Name = "Tina", age = $, Address = "Shanghai"});
Customs. ADD (New Custom () {Name = "Luo", age = +, Address = "Beijing"});
Customs. ADD (New Custom () {Name = "Wang", age = $, Address = "Guangdong"});
Customs. ADD (New Custom () {Name = "Feng", age = +, Address = "Yunnan"});
}

     stopWatch.Restart();     var groupByAge = customs.GroupBy(item => item.Age).ToList();     foreach (var item in groupByAge)     {        Console.WriteLine("Age={0},count = {1}", item.Key, item.Count());     }     stopWatch.Stop();     Console.WriteLine("Linq group by time is: " + stopWatch.ElapsedMilliseconds);     stopWatch.Restart();     var lookupList = customs.ToLookup(i => i.Age);     foreach (var item in lookupList)     {        Console.WriteLine("LookUP:Age={0},count = {1}", item.Key, item.Count());     }     stopWatch.Stop();     Console.WriteLine("LookUp group by time is: " + stopWatch.ElapsedMilliseconds);  }

Copy Code

The results of the operation are as follows:

The ToLookup method is to convert a collection to a read-only collection, so performance is better than list when large data packets are grouped. You can consult the relevant information, here because of the space problem, no longer elaborate.

C # parallel and buy-in platforms build parallel collections and PLINQ

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.