谁能告诉我正确的 Plinq 代码是什么?我将双精度数组的每个元素的正弦绝对值的平方根相加,但 Plinq 给了我错误的结果。
该程序的输出是:
Linq 聚合 = 75.8310477905274(正确) Plinq 聚合 = 38.0263653589291(大约应该是一半)
我一定是做错了什么,但我不知道是什么...
(我在 Core 2 Duo Windows 7 x64 PC 上使用 Visual Studio 2008 运行它。)
这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
double[] array = new double[100];
for (int i = 0; i < array.Length; ++i)
{
array[i] = i;
}
double sum1 = array.Aggregate((total, current) => total + Math.Sqrt(Math.Abs(Math.Sin(current))));
Console.WriteLine("Linq aggregate = " + sum1);
IParallelEnumerable<double> parray = array.AsParallel<double>();
double sum2 = parray.Aggregate((total, current) => total + Math.Sqrt(Math.Abs(Math.Sin(current))));
Console.WriteLine("Plinq aggregate = " + sum2);
}
}
}