对内存中的数据做并行运算,

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();

  

 

相关文章:

  • 2021-12-09
  • 2021-11-03
  • 2021-11-10
  • 2021-08-24
  • 2021-10-29
  • 2021-05-05
  • 2022-02-10
猜你喜欢
  • 2019-05-20
  • 2021-07-19
  • 2021-11-05
  • 2021-05-06
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案