• 并行速度比较


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

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

      

  • 相关阅读:
    MapReduce TFIDF 案列
    MapReduce PageRank案列
    MapReduce好友推荐案例
    MapReduce天气查询实列
    MapReduce源码分析
    Tiny6410之LED裸机驱动
    Linux -- objdump (待继续完善)
    Linux -- xxd 整理自man 手册 (MARK)
    Linux -- xxd (转)
    tar -- 打包压缩文件
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/10372561.html
Copyright © 2020-2023  润新知