zoukankan      html  css  js  c++  java
  • 并行速度比较

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

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

      

  • 相关阅读:
    Redis数据结构之字典
    多路复用
    Redis数据结构之SDS
    记一个图片转换神器vectorizer
    Java基础之面向对象上
    科学
    Linux内核源码分析之setup_arch (二)
    Linux内核源码分析之setup_arch (一)
    printk 流程分析
    多个线程顺序打印问题,一网打尽
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/10372561.html
Copyright © 2011-2022 走看看