zoukankan      html  css  js  c++  java
  • Parallel小记

     1         List<Temp> tList = new List<Temp>();
     2             for (int i = 1; i < 1000; i++)
     3             {
     4                 tList.Add(new Temp()
     5                 {
     6                     id = i,
     7                     name = string.Concat("test", i),
     8                     age = i * 2
     9                 });
    10             }
    11             object obj = new object();
    12             List<TempChild> resultList = new List<TempChild>();
    13             Stopwatch sc = new Stopwatch();
    14             Stopwatch sc1 = new Stopwatch();
    15             Stopwatch sc2 = new Stopwatch();
    16             sc.Start();
    17             Parallel.ForEach(tList, item =>
    18             {
    19                 List<TempChild> tempChildList = LoadChildList();
    20                 if (null != tempChildList && tempChildList.Count > 0)
    21                 {
    22                     lock (obj)//一定要加锁,不然会出异常【目标数组长度不够,请检查destIndex和长度以及数组下限】
    23                     {
    24                         resultList.AddRange(tempChildList);
    25                     }
    26                 }
    27             });
    28             sc.Stop();
    29             long second = sc.ElapsedMilliseconds;
    30             List<TempChild> resultList1 = new List<TempChild>();
    31             sc1.Start();
    32             foreach (var item in tList)
    33             {
    34                 List<TempChild> tempChildList = LoadChildList();
    35                 if (null != tempChildList && tempChildList.Count > 0)
    36                 {
    37                     resultList1.AddRange(tempChildList);
    38                 }
    39             }
    40             sc1.Stop();
    41             long second2 = sc1.ElapsedMilliseconds;
    42 
    43             sc2.Start();
    44             List<TempChild> resultList2 = tList
    45                 .AsParallel()
    46                 .SelectMany(
    47                 (item) =>
    48                 {
    49                     return LoadChildList();
    50                 }).ToList();
    51             sc2.Stop();
    52             long second3 = sc2.ElapsedMilliseconds;
       private static List<TempChild> LoadChildList()
            {
                List<TempChild> tList = new List<TempChild>();
                for (int i = 1; i < 100; i++)
                {
                    tList.Add(new TempChild()
                    {
                        id = i,
                        name = string.Concat("test", i),
                        age = i * 2,
                        addr = i.ToString()
                    });
                }
                return tList;
            }
    

      分别对不同数据量级的并行任务执行对比,三种方式结果如下:

      

      

      

      

      

      百万数量级,电脑崩了,没有测试结果~@@!!

      由此,一般的list没必要并行执行,foreach足以。

  • 相关阅读:
    思考未来:你的目标是什么
    从非同凡响开始:绝不要做他人也在做的事
    Elasticsearch的内置分词器
    Elasticsearch倒排索引的核心组成
    Session 与 JWT
    vue全屏组件
    css弹性盒骰子
    es6模块化
    移动端适配
    echarts-3D地图
  • 原文地址:https://www.cnblogs.com/lb12081116/p/6644877.html
Copyright © 2011-2022 走看看