我在 C# 中编写了一个并行算法,将一个数组划分为两个列表,一个包含满足给定谓词的元素,另一个列表包含不满足谓词的元素。它是一种保序算法。
我写了如下,但我想知道如何最大化从硬件并发中获利的机会。
static void TestPLinqPartition(int cnt = 1000000)
{
Console.WriteLine("PLINQ Partition");
var a = RandomSequenceOfValuesLessThan100(cnt).ToArray();
var sw = new Stopwatch();
sw.Start();
var ap = a.AsParallel();
List<int> partA = null;
List<int> partB = null;
Action actionA = () => { partA = (from x in ap where x < 25 select x).ToList(); };
Action actionB = () => { partB = (from x in ap where !(x < 25) select x).ToList(); };
Parallel.Invoke(actionA, actionB);
sw.Stop();
Console.WriteLine("Partion sizes = {0} and {1}", partA.Count, partB.Count);
Console.WriteLine("Time elapsed = {0} msec", sw.ElapsedMilliseconds);
}