Ejemplo n.º 1
0
        public ComparisonTestSettings LoadComparisonSettings()
        {
            Console.ForegroundColor = ConsoleColor.White;
            int       testSize   = loadValue("Please enter the test set size:", 1000000);
            int       testNumber = loadValue("Please enter the tests number:", 1);
            int       mode       = loadValue("Please select Radix sort (1 - LSD; 2 - MSD):", 2);
            RadixType radix;

            if (mode == 1)
            {
                Console.WriteLine("Using LSD Radix sort.");
                radix = RadixType.LSD;
            }
            else if (mode == 2)
            {
                Console.WriteLine("Using MSD Radix sort.");
                radix = RadixType.MSD;
            }
            else
            {
                Console.WriteLine("Using MSD Radix sort (default).");
                radix = RadixType.MSD;
            }

            int maxValue = int.MaxValue;

            if (radix == RadixType.LSD)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("The monotonic-sort implementation boosts performance by comparing only the relevant bytes of the monotonic values.");
                Console.WriteLine("e.g.: If the maximal monotonic value binary representation does not use the left-most byte, none of the other monotonic value uses it and it can be skipped.");
                Console.WriteLine("This feature is applicable only to non-negative monotonic values.");
                Console.ForegroundColor = ConsoleColor.White;
                maxValue = loadValue("Please enter maximal possible value (optional):");
            }

            ComparisonTestSettings res = new ComparisonTestSettings(testSize, testNumber, maxValue, radix);

            return(res);
        }
Ejemplo n.º 2
0
        public void RunComparison(ComparisonTestSettings settings)
        {
            TimeSpan monotonicTimes = TimeSpan.Zero;
            TimeSpan introTimes     = TimeSpan.Zero;

            Console.WriteLine($"Running a sequence of {settings.Count} with input size: {settings.Size}.");

            moObject[] elements   = new moObject[settings.Size];
            Random     randomizer = new Random();

            for (int t = 0; t < settings.Count; t++)
            {
                Console.WriteLine($"Starting test #{t + 1}.");
                Console.WriteLine($"Randomizing {settings.Size} items.");
                long startMemory = GC.GetTotalMemory(false);
                for (int j = 0; j < settings.Size; j++)
                {
                    int      currentRandomValue = randomizer.Next(settings.MaxValue);
                    moObject currentValue       = new moObject(currentRandomValue);
                    elements[j] = currentValue;
                }

                long consumedMemory = GC.GetTotalMemory(false) - startMemory;
                Console.WriteLine($"Memory consumed by input array: {consumedMemory} bytes.");
                monotonicTimes += runMethod("monotonic-sort", (array) =>
                {
                    MonotonicSorter mfSort = new MonotonicSorter();
                    mfSort.Sort(array, settings.Radix);
                }, elements);
                introTimes += runMethod("built-in intro-sort", (array) =>
                {
                    Array.Sort(array);
                }, elements);
            }

            Console.WriteLine($"Results for data sets of size: {settings.Size}");
            Console.WriteLine($"The average monotonic sort took: {monotonicTimes.TotalMilliseconds / settings.Count} ms.");
            Console.WriteLine($"The average built-in introsort took: {introTimes.TotalMilliseconds / settings.Count} ms.");
            Console.WriteLine($"Time ratio: introsort/monotonic-sort: {introTimes.TotalMilliseconds / monotonicTimes.TotalMilliseconds}");
        }