简单介绍:
此处介绍的并行处理,主要是处理本地存储的数据;当使用并行处理时,会把数据拆分为多个小块,然后用多个线程处理这些小块的数据,多线程处理后的数据再统一处理再返回;
以下是处理100万数组的数据量;代码如下:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace _Console { class Program { static void Main(string[] args) { Go(); Console.ReadKey(); } public static void Go() { List<int> array = new List<int>(); int len = 1000000; for (int i = 0; i < len; i++) { array.Add(i); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var parallelResult = from n in array.AsParallel() where n % 2 > 0 select n; long ddd = stopwatch.ElapsedMilliseconds; stopwatch.Stop(); Console.WriteLine($"并行时间:{ddd}毫秒"); stopwatch.Start(); var parallelResultLinq = from n in array where n % 2 > 0 select n; long ddd1 = stopwatch.ElapsedMilliseconds; stopwatch.Stop(); Console.WriteLine($"非并行时间:{ddd1}毫秒"); } } }
执行的结果如下: