none
关于并行Linq,手动分区. RRS feed

  • 问题

  • static void Main(string[] args) {
                int arraySize = 100000000;
                int[] intArray = new int[arraySize];
                Random r = new Random();    //随机给数组赋值.
                for(int i = 0; i < intArray.Length; i++) {
                    intArray[i] = r.Next(100);
                }
                DateTime start = DateTime.Now;  //开始计时.
                var query = (from x in intArray //方式(一):普通模式.
                             where x < 50
                             select x).Sum();
                DateTime part = DateTime.Now;    //结束计时.
                Console.WriteLine("耗时: " + (part - start).TotalSeconds);
                //耗时: 2.203126.
                //自动分区:
                var query2 = (from x in intArray.AsParallel()   //方式(二):默认情况下的平行Linq.
                             where x < 50
                             select x).Sum();
                DateTime end = DateTime.Now;    //结束计时.
                Console.WriteLine("耗时: " + (end - part).TotalSeconds);
                //耗时: 1.503086.
                //手动分区:
                var queryManual = (from x in Partitioner.Create(intArray, true).AsParallel()    //手动分区的平行Linq.
                                   where x < 50
                                   select x).Sum();
                DateTime end2 = DateTime.Now;    //结束计时.
                Console.WriteLine("耗时: " + (end2 - end).TotalSeconds);
                //耗时: 2.2711299.
                Console.ReadKey();
            }
    //大家懂的...
    这三种方式...
    我郁闷的是,手动分区的耗时和普通模式耗时一样的...
    是不是这不是分区哈...如果是分区...都没有用的..赤裸裸的多余...



    2013年6月15日 6:17

答案

  • 一般只需要使用AsParallel进行自动产生线程自动计算,无须使用Partition分区机制。

    而分区往往和Parallel.ForEach使用,对每一块自身数据都是很大的情况下很适合:

    namespace Csharp
    {
        public class HeaveyWork
        {
            public static Random rd = new Random(DateTime.Now.GetHashCode());
     
            public void Wait()
            {
                //长时间工作
                Thread.Sleep(rd.Next(10,100));
            }
        }
        public class Program
        {
            static void Main(string[] args)
            {
                HeaveyWork[]works = new HeaveyWork[100];
                for (int i = 0i < works.Lengthi++)
                {
                    works[i= new HeaveyWork();
                }
                Stopwatch watch = new Stopwatch();
                watch.Start();
                Parallel.ForEach<HeaveyWork>(Partitioner.Create(workstrue), (work=> work.Wait());
                watch.Stop();
                Console.WriteLine(watch.ElapsedMilliseconds);
            }
        }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月15日 10:11
    版主