public static void SimpleNotInPlaceExample() { 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) }; UserDefinedClass[] sortedUserArray; Console.Write("Unsorted array of user defined class: "); foreach (UserDefinedClass item in userArray) { Console.Write(item); } Console.WriteLine(); sortedUserArray = userArray.OrderBy(element => element.Key).ToArray(); // Serial Linq Sort (C# standard sort, stable) sortedUserArray = userArray.SortRadix(element => element.Key); // Serial Radix Sort (stable) sortedUserArray = Algorithm.SortMerge(userArray, comparer); // Serial Merge Sort (stable) sortedUserArray = userArray.AsParallel().OrderBy(element => element.Key).ToArray(); // Parallel Linq Sort (C# standard sort, stable) sortedUserArray = ParallelAlgorithm.SortMergePar(userArray, comparer); // Parallel Merge Sort sortedUserArray = ParallelAlgorithm.SortMergeStablePar(userArray, comparer); // Parallel Merge Sort (stable) Console.Write("Sorted array of user defined class: "); foreach (UserDefinedClass item in sortedUserArray) { Console.Write(item); } Console.WriteLine(); }
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.SortRadix2(element => element.Key); // faster, uses more memory //sortedArrayOne = benchArrayOne.SortRadix(element => element.Key); // slower, uses less memory } } else { benchArrayOne.SortMergeInPlacePar(comparer); } } else { if (!parallel) { if (!radixSort) { sortedArrayOne = benchArrayOne.SortMerge(comparer); } else { sortedArrayOne = benchArrayOne.SortRadix2(element => element.Key); // faster, uses more memory //sortedArrayOne = benchArrayOne.SortRadix2(element => element.Key); // slower, user less memory } } else { if (!radixSort) { sortedArrayOne = benchArrayOne.SortMergeStablePar(comparer); } else { sortedArrayOne = benchArrayOne.SortRadix2(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); } } } }