예제 #1
0
        public static void SimpleInPlaceExample()
        {
            var comparer = new UserDefinedClassComparer();

            UserDefinedClass[] userArray = new UserDefinedClass[]
            {
                new UserDefinedClass(16, 0),
                new UserDefinedClass(12, 1),
                new UserDefinedClass(18, 2),
                new UserDefinedClass(18, 3),
                new UserDefinedClass(10, 4),
                new UserDefinedClass(2, 5)
            };
            Console.Write("Unsorted array of user defined class: ");
            foreach (UserDefinedClass item in userArray)
            {
                Console.Write(item);
            }
            Console.WriteLine();

            Array.Sort(userArray, comparer);                        // Serial   Array.Sort (serial, not stable)
            Algorithm.SortMergeInPlace(userArray, comparer);        // Serial   Merge Sort. Direct function call syntax (serial, stable)

            userArray.SortMergeInPlace(comparer);                   // Serial   Merge Sort. Extension method syntax (serial, stable)
            userArray.SortMergeInPlaceAdaptivePar(comparer);        // Parallel Merge Sort (parallel, not stable)
            userArray.SortMergePseudoInPlaceStablePar(comparer);    // Parallel Merge Sort (parallel, stable)

            Console.Write("Sorted   array of user defined class: ");
            foreach (UserDefinedClass item in userArray)
            {
                Console.Write(item);
            }
            Console.WriteLine();
        }
예제 #2
0
        public static void SortMeasureArrayOfUserDefinedClassSpeedup(bool parallel, bool vsLinq, bool radixSort)
        {
            Random randNum   = new Random(5);
            int    arraySize = 1 * 1000 * 1000;

            var comparer       = new UserDefinedClassComparer();
            var equal          = new UserDefinedClassEquality();
            var benchArrayOne  = new UserDefinedClass[arraySize];
            var benchArrayTwo  = new UserDefinedClass[arraySize];
            var sortedArrayOne = new UserDefinedClass[arraySize];
            var sortedArrayTwo = new UserDefinedClass[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                var randomValue = (uint)randNum.Next(0, Int32.MaxValue);
                benchArrayOne[i] = new UserDefinedClass(randomValue, i);
                //benchArrayOne[i] = new UserDefinedClass((uint)i, i);           // fill array with incrementing values
                //benchArrayOne[i] = new UserDefinedClass((uint)5, i);           // fill array with constant     values
            }
            for (int i = 0; i < arraySize; i++)
            {
                benchArrayTwo[i] = new UserDefinedClass(benchArrayOne[i].Key, benchArrayOne[i].Index);
            }
            Stopwatch stopwatch      = new Stopwatch();
            long      frequency      = Stopwatch.Frequency;
            long      nanosecPerTick = (1000L * 1000L * 1000L) / frequency;

            stopwatch.Restart();
            if (!vsLinq)
            {
                if (!parallel)
                {
                    if (!radixSort)
                    {
                        benchArrayOne.SortMergeInPlace(comparer);
                    }
                    else
                    {
                        sortedArrayOne = benchArrayOne.SortRadixFaster(element => element.Key);   // faster, uses more memory
                        //sortedArrayOne = benchArrayOne.SortRadix(element => element.Key);  // slower, uses less memory
                    }
                }
                else
                {
                    benchArrayOne.SortMergeInPlaceAdaptivePar(comparer);
                }
            }
            else
            {
                if (!parallel)
                {
                    if (!radixSort)
                    {
                        sortedArrayOne = benchArrayOne.SortMerge(comparer);
                    }
                    else
                    {
                        sortedArrayOne = benchArrayOne.SortRadixFaster(element => element.Key);      // faster, uses more memory
                        //sortedArrayOne = benchArrayOne.SortRadix(element => element.Key);    // slower, user less memory
                    }
                }
                else
                {
                    if (!radixSort)
                    {
                        sortedArrayOne = benchArrayOne.SortMergeStablePar(comparer);
                    }
                    else
                    {
                        sortedArrayOne = benchArrayOne.SortRadixFaster(element => element.Key);      // faster, uses more memory
                        //sortedArrayOne = benchArrayOne.SortRadix(element => element.Key);     // slower, uses less memory
                    }
                }
            }
            stopwatch.Stop();
            double timeMergeSort = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

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

            if (!vsLinq)
            {
                if (!radixSort)
                {
                    bool equalSortedArrays = benchArrayOne.SequenceEqual(benchArrayTwo, equal);
                    if (!equalSortedArrays)
                    {
                        Console.WriteLine("Sorting results using Merge Sort are not equal!");
                    }
                }
                else
                {
                    bool equalSortedArrays = benchArrayTwo.SequenceEqual(sortedArrayOne, equal);
                    if (!equalSortedArrays)
                    {
                        Console.WriteLine("Sorting results using Radix Sort are not equal!");
                    }
                }
            }
            else
            {
                bool equalSortedArrays = sortedArrayOne.SequenceEqual(sortedArrayTwo, equal);
                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, Serial   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, Serial   Radix Sort {2:0.000} sec, speedup {3:0.00}", arraySize,
                                          timeArraySort, timeMergeSort, timeArraySort / timeMergeSort);
                    }
                }
            }
        }