First look at the way LINQ is, the dynamic way:
void Main () {//testing Setupvar Source = Enumerable.range (0, 10000000). ToArray ();d ouble[] results = new Double[source. Length]; Console.WriteLine ("Creating Partitioner in LINQ ..."); var dt = Datetime.now;var Partitionerlinq = partitioner.create ( source, True); Console.WriteLine ("Creating Partitioner in LINQ done, ticks:" + (DATETIME.NOW-DT). Ticks);d T = datetime.now;var r = Partitionerlinq.asparallel (). Select (x = x * x * math.pi). ToList (); Console.WriteLine ("LINQ to-do, ticks:" + (DATETIME.NOW-DT). Ticks);d t = datetime.now;for (var i = 0;i < source. Length; i++) {Results[i] = source[i] * MATH.PI;} Console.WriteLine ("Processing by single thread done, ticks:" + (DATETIME.NOW-DT). Ticks);}
LINQPad Output:
Creating Partitioner in LINQ way...creating Partitioner in LINQ done, Ticks:0linq-do, ticks:3472754processing by S Ingle Thread Done, ticks:380298
It can be seen that the partition of dynamically allocated chunk is 10 times times slower than the ordinary implementation of single-thread.
Then look at the static way:
void Main () {//testing Setupvar Source = Enumerable.range (0, 10000000). ToArray ();d ouble[] results = new Double[source. Length]; Console.WriteLine ("Creating Partitioner in a-processing as static range ..."); var dt = datetime.now;//Partition the entire source Array.var Rangepartitioner = partitioner.create (0, source. Length); Console.WriteLine ("Created Partitioner, Ticks:" + (DATETIME.NOW-DT). Ticks);d t = datetime.now;//Loop over the partitions in parallel. Parallel.ForEach (Rangepartitioner, (range, loopstate) =>{//Loop over each range element without a delegate invocation. for (int i = range. Item1; I < range. ITEM2; i++) {Results[i] = source[i] * source[i] * MATH.PI;}); Console.WriteLine ("Processing by range partitioner done, ticks:" + (DATETIME.NOW-DT). Ticks);d t = datetime.now;for (var i = 0;i < source. Length; i++) {Results[i] = source[i] * MATH.PI;} Console.WriteLine ("Processing by single thread done, ticks:" + (DATETIME.NOW-DT). Ticks);} Define other methods and classES here
LINQPad Output:
Creating Partitioner in a-processing as static range ... Created Partitioner, ticks:0processing by range Partitioner do, ticks:185180processing by single thread done, ticks: 375279
Basically, static partition is twice times faster than the normal way.
Now, still using static mode, increase the amount of data by 10 times times to see the LINQ output:
Creating Partitioner in a-processing as static range ... Created Partitioner, ticks:0processing by range Partitioner do, ticks:1951457processing by single thread done, ticks: 3808162
It can be seen that the time difference is still almost one-fold speed.
In summary, it is best to use static mode when considering partition, and the processing logic is not recommended too complex (MSDN Recommendation: In general, range partitioning was only faster when the execution time of The delegate is small to moderate); but this is still to be tested, and for the time being, there is no suitable logic to test.
Performance comparison of Partitioner static and dynamic in. Net