标签:
void Main() { // testing setup var source = Enumerable.Range(0, 10000000).ToArray(); double[] results = new double[source.Length]; Console.WriteLine("creating partitioner in LINQ way..."); var dt = DateTime.Now; var partitionerLinq = Partitioner.Create(source, true); Console.WriteLine("creating partitioner in LINQ done, ticks: " + (DateTime.Now - dt).Ticks); dt = DateTime.Now; var r = partitionerLinq.AsParallel().Select(x => x * x * Math.PI).ToList(); Console.WriteLine("LINQ way done , ticks:" + (DateTime.Now - dt).Ticks); dt = 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); }
creating partitioner in LINQ way... creating partitioner in LINQ done, ticks: 0 LINQ way done , ticks:3472754 processing by single thread done, ticks : 380298
void Main() { // testing setup var source = Enumerable.Range(0, 10000000).ToArray(); double[] results = new double[source.Length]; Console.WriteLine("creating partitioner in a way 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); dt = 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); dt = 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
creating partitioner in a way processing as static range .. created partitioner, ticks:0 processing by range partitioner done, ticks : 185180 processing by single thread done, ticks : 375279
creating partitioner in a way processing as static range .. created partitioner, ticks:0 processing by range partitioner done, ticks : 1951457 processing by single thread done, ticks : 3808162
.Net 中Partitioner static与dynamic的性能对比
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/50298371