zoukankan      html  css  js  c++  java
  • 并行处理测试

    class Program
        {
            static void Main(string[] args)
            {
                List<entityA> source = new List<entityA>();
                for (int i = 0; i < 20; i++)
                {
                    source.Add(new entityA
                    {
                        name = "悟空" + i,
                        sex = i % 2 == 0 ? "" : "",
                        age = i
                    });
                }
    
                Stopwatch Watch3 = new Stopwatch();
                Watch3.Start();
                loop2(source);
                Watch3.Stop();
                Console.WriteLine("一般foreach循环耗时:" + Watch3.ElapsedMilliseconds);
                
    
                Stopwatch Watch5 = new Stopwatch();
                Watch5.Start();
                loop4(source);
                Watch5.Stop();
                Console.WriteLine("并行foreach循环耗时:" + Watch5.ElapsedMilliseconds);
                Console.ReadLine();
            }
    
            //普通的foreach循环
            static void loop2(List<entityA> source)
            {
                foreach (entityA item in source)
                {
                    item.age = +10;
                    System.Threading.Thread.Sleep(1000);//毫秒
                }
            }
    
            //并行的foreach循环
            static void loop4(List<entityA> source)
            {
                Parallel.ForEach(source,new ParallelOptions { MaxDegreeOfParallelism=Environment.ProcessorCount}, item =>
                {
                    item.age = item.age + 10;
                    System.Threading.Thread.Sleep(1000);
                });
            }
            //                    0   5   10   100   500    10000
            //一般foreach循环耗时 0   119 218  2011  10000  20010
            //并行foreach循环耗时 97  68  92   564   2500   5080
    
        }
        //简单的实体
        class entityA
        {
            public string name { set; get; }
            public string sex { set; get; }
            public int age { set; get; }
        }
  • 相关阅读:
    web(零)---tornado使用
    web(一)----tornado nginx配置
    pb- 使用
    Python排序算法之直接插入排序
    敏捷测试中发现的一些问题及改进办法
    加密算法
    共享内存与存储映射(mmap)
    mysql索引的性能分析
    mysql索引
    Innodb的存储及缓存
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/11021133.html
Copyright © 2011-2022 走看看