对内存中的数据做并行运算,
AsParallel(并行化)
就是在集合后加个AsParallel()
性能比较: 并行用了27秒,不用并行用了33秒
var Elements = EleList.Where(m =>
{
foreach (var rule in tmpRules)
{
rule.Status = false;
if (!rule.Match(m))
{
break;
}
}
return tmpRules.Where(r => r.Status == false).Count() == 0;
}).AsParallel().ToList();
使用QueueUserWorkItem,速度又有所提升,大概10秒左右
var doneEvent = new ManualResetEvent(false); int taskCount = 0; for (int i = 0; i < filteredEleList.Count; i++) { int index = i; Interlocked.Increment(ref taskCount); matchTmpRules[index] = tmpRules; ThreadPool.QueueUserWorkItem((object evt) => { try { var field = filteredEleList[index]; foreach (var rule in matchTmpRules[index]) { rule.Status = false; if (!rule.Match(field)) { break; } } if(tmpRules.Where(r => r.Status == false).Count() == 0) { tmpElements.Add(field); } } catch (Exception ex) { //Logger.Write(ex, "There was an error while trying to match field '" + fields[index].Name + "'."); } finally { if (Interlocked.Decrement(ref taskCount) == 0) { doneEvent.Set(); } } }, null); } doneEvent.WaitOne();