我试图想出一种方法来加速以最快的方式将列表中包含的大量物体组合起来。 希望利用 PLINQ 我尝试过, 但这不是一个线索安全的解决办法 。 我在 VS2010 和 VS11Beta 中测试了 4. 0 和 4. 5 和 4. 5 。 这是我的试样应用程序。 如果您在 1500 之间更改 BlowUp () 通常会有效 。 在500 轮子到达轨道后, 它会在许多地方失效 。 有人知道最快的方法解决这个问题吗? (多层阵列+ PLINQ? )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PLinqBlowsUp
{
class Program
{
static void Main(string[] args)
{
BlowUp(5000);
}
private static void BlowUp(int blowupNum)
{
try
{
var theExistingMasterListOfAllRowsOfData = new List<List<KeyValuePair<string, dynamic>>>();
//Add some test data
Enumerable.Range(0, blowupNum).AsParallel().ForAll(row => theExistingMasterListOfAllRowsOfData.Add(AddRowToMasterList(row)));
var aNewRowOfData = new List<KeyValuePair<string, dynamic>>();
//Add some test data
var column = new KeyValuePair<string, dynamic>("Title", "MyTitle");
aNewRowOfData.Add(column);
var anotherNewRowOfData = new List<KeyValuePair<string, dynamic>>();
//Add some test data
var columnA = new KeyValuePair<string, dynamic>("Date", DateTime.Now);
var columnB = new KeyValuePair<string, dynamic>("ImportantColumn", "ImportantData");
var columnC = new KeyValuePair<string, dynamic>("VeryImportantColumn", "VeryImportantData");
anotherNewRowOfData.Add(columnA);
anotherNewRowOfData.Add(columnB);
anotherNewRowOfData.Add(columnC);
//Now the Problem
aNewRowOfData.AsParallel().ForAll(anrod => theExistingMasterListOfAllRowsOfData.ForEach(temloarod => temloarod.Add(anrod)));
anotherNewRowOfData.AsParallel().ForAll(anrod => theExistingMasterListOfAllRowsOfData.ForEach(temloarod => temloarod.Add(anrod)));
//Test for number
foreach (var masterRow in theExistingMasterListOfAllRowsOfData)
{
if (masterRow.Count != 7)
throw new Exception("BLOW UP!!!");
}
}
catch (AggregateException ex)
{
Console.WriteLine(ex.Message);
}
}
private static List<KeyValuePair<string, dynamic>> AddRowToMasterList(int row)
{
var columnA = new KeyValuePair<string, dynamic>("FirstName", "John" + row.ToString());
var columnB = new KeyValuePair<string, dynamic>("LastName", "Smith" + row.ToString());
var columnC = new KeyValuePair<string, dynamic>("Ssn", 123456789 + (row*10));
var list = new List<KeyValuePair<string, dynamic>>();
list.Add(columnA);
list.Add(columnB);
list.Add(columnC);
return list;
}
}
}