PLINQ (Parallel LINQ) of C# Parallel Programming_C# Tutorial

Used to perform parallel operations on data in memory, that is to say, it only supports parallel operations of LINQ to Object

1. AsParallel (parallelization)

Just add an AsParallel() after the collection.

For example:

var numbers = Enumerable.Range(0, 100);
var result = numbers.AsParallel().AsOrdered().Where(i => i % 2 == 0);
foreach (var i in result)
Console.WriteLine(i);

Next, let's simulate pouring 15 million records into ConcurrentDictionary to see the difference between serial and parallel efficiency. Note that my classic machine has 2 hardware threads.

static void Main(string[] args)
{
    var dic = LoadData();

    Stopwatch watch = new Stopwatch();

    watch.Start();

    //串行执行
    var query1 = (from n in dic.Values
                  where n.Age > 20 && n.Age < 25
                  select n).ToList();

    watch.Stop();

    Console.WriteLine("串行计算耗费时间:{0}", watch.ElapsedMilliseconds);

    watch.Restart();

    var query2 

Guess you like

Origin blog.csdn.net/shengyin714959/article/details/130329683