示例#1
0
        public static void SortMeasureArraySpeedup(bool parallel, bool vsLinq, bool radixSort)
        {
            Random randNum   = new Random(5);
            int    arraySize = 1 * 1000 * 1000;

            uint[] benchArrayOne  = new uint[arraySize];
            uint[] benchArrayTwo  = new uint[arraySize];
            uint[] sortedArrayOne = new uint[arraySize];
            uint[] sortedArrayTwo = new uint[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                benchArrayOne[i] = (uint)randNum.Next(0, Int32.MaxValue);    // fill array with random       values between min and max
                //benchArrayOne[i] = (uint)i;                                // fill array with incrementing values
                //benchArrayOne[i] = 0;                                      // fill array with constant     values
                benchArrayTwo[i] = benchArrayOne[i];
            }

            Stopwatch stopwatch      = new Stopwatch();
            long      frequency      = Stopwatch.Frequency;
            long      nanosecPerTick = (1000L * 1000L * 1000L) / frequency;

            stopwatch.Restart();
            if (!vsLinq)
            {
                if (!parallel)
                {
                    if (!radixSort)
                    {
                        benchArrayOne.SortMergeInPlace();
                    }
                    else
                    {
                        sortedArrayOne = benchArrayOne.SortRadix();
                    }
                }
                else
                {
                    if (!radixSort)
                    {
                        benchArrayOne.SortMergeInPlacePar();
                    }
                    else
                    {
                        sortedArrayOne = benchArrayOne.SortRadixPar();
                    }
                }
            }
            else
            {
                if (!parallel)
                {
                    if (!radixSort)
                    {
                        sortedArrayOne = benchArrayOne.SortMerge();
                    }
                    else
                    {
                        sortedArrayOne = benchArrayOne.SortRadix();
                    }
                }
                else
                {
                    if (!radixSort)
                    {
                        sortedArrayOne = benchArrayOne.SortMergePar();          // Stable sorting is not necessary for array of integers
                        //sortedArrayOne = benchArrayOne.SortMergeStablePar();
                    }
                    else
                    {
                        //sortedArrayOne = benchArrayOne.SortRadix();
                        sortedArrayOne = benchArrayOne.SortRadixPar();
                    }
                }
            }
            stopwatch.Stop();
            double timeMergeSort = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

            stopwatch.Restart();
            if (!vsLinq)
            {
                Array.Sort(benchArrayTwo);
            }
            else
            {
                if (parallel)
                {
                    sortedArrayTwo = benchArrayTwo.AsParallel().OrderBy(element => element).ToArray();
                }
                else
                {
                    sortedArrayTwo = benchArrayTwo.OrderBy(element => element).ToArray();
                }
            }
            stopwatch.Stop();
            double timeArraySort = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

            if (!vsLinq)
            {
                if (!radixSort)
                {
                    bool equalSortedArrays = benchArrayOne.SequenceEqual(benchArrayTwo);
                    if (!equalSortedArrays)
                    {
                        Console.WriteLine("Sorting results using Merge Sort are not equal!");
                    }
                }
                else
                {
                    bool equalSortedArrays = benchArrayTwo.SequenceEqual(sortedArrayOne);
                    if (!equalSortedArrays)
                    {
                        Console.WriteLine("Sorting results using Radix Sort are not equal!");
                    }
                }
            }
            else
            {
                bool equalSortedArrays = sortedArrayOne.SequenceEqual(sortedArrayTwo);
                if (!equalSortedArrays)
                {
                    Console.WriteLine("Sorting results using Merge Sort are not equal!");
                }
            }

            if (!vsLinq)
            {
                if (!parallel)
                {
                    if (!radixSort)
                    {
                        Console.WriteLine("C# array of size {0}: Array.Sort              {1:0.000} sec, Serial   Merge Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                    else
                    {
                        Console.WriteLine("C# array of size {0}: Array.Sort              {1:0.000} sec, Serial   Radix Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                }
                else
                {
                    if (!radixSort)
                    {
                        Console.WriteLine("C# array of size {0}: Array.Sort              {1:0.000} sec, Parallel Merge Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                    else
                    {
                        Console.WriteLine("C# array of size {0}: Array.Sort              {1:0.000} sec, Parallel Radix Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                }
            }
            else
            {
                if (!parallel)
                {
                    if (!radixSort)
                    {
                        Console.WriteLine("C# array of size {0}: Linq.OrderBy            {1:0.000} sec, Serial   Merge Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                    else
                    {
                        Console.WriteLine("C# array of size {0}: Linq.OrderBy            {1:0.000} sec, Serial   Radix Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                }
                else
                {
                    if (!radixSort)
                    {
                        Console.WriteLine("C# array of size {0}: Linq.OrderBy.AsParallel {1:0.000} sec, Parallel Merge Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                    else
                    {
                        Console.WriteLine("C# array of size {0}: Linq.OrderBy.AsParallel {1:0.000} sec, Parallel Radix Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                }
            }
        }