示例#1
0
        static void Main(string[] args)
        {
            // Print starting message
            Console.WriteLine("Starting tests for MergeSort vs Threaded MergeSort.");

            // Declare variables needed
            List <int> listLengths = new List <int> {
                8, 64, 256, 1024
            };
            long startTime = 0, endTime = 0;

            foreach (int length in listLengths)
            {
                // 1. Create the randomized list
                List <int> toBeSorted = new List <int>(length);                   // create list with initial capacity 'length'
                RandomIntListGenerator.Fill(ref toBeSorted, 0, Int32.MaxValue);   // randomly fill list with values from 0 to Int32.MaxValue
                List <int> toBeSorted2 = new List <int>(toBeSorted);              // create identical copy

                // 2. Test the different sorting methods for each 'length'
                // declare sorting objects
                MergeSort <int>         mergeSort         = new MergeSort <int>();
                ThreadedMergeSort <int> threadedMergeSort = new ThreadedMergeSort <int>();
                // print starting message
                Console.WriteLine(String.Format("Sorting for size {0}.", length));
                // time the sort
                startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                mergeSort.Sort(toBeSorted);
                endTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                // print off the sorting's time
                Console.WriteLine(String.Format("\tNormal Sort Time: {0} ms", endTime - startTime));
                // print list for debugging to see if it's actually sorted
                //printList(toBeSorted);
                // time the threaded sort
                startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                threadedMergeSort.Sort(toBeSorted2);
                endTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                // print off the threaded sorting's time
                Console.WriteLine(String.Format("\tThreaded Sort Time: {0} ms", endTime - startTime));
                // print list for debugging to see if it's actually sorted
                //printList(toBeSorted2);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            List <int> unsorted = new List <int>();
            List <int> sorted;
            List <int> unsortedList = new List <int>();
            //number of threads
            int numbers = 2000;

            int[] unsortedThreadedList = new int[numbers];

            Stopwatch stopWatch1 = new Stopwatch();
            Stopwatch stopWatch2 = new Stopwatch();



            Random random = new Random();


            //generate random number list to sort
            Console.WriteLine("Original array elements:");
            for (int i = 0; i < numbers; i++)
            {
                int newVal = random.Next(0, numbers * 2);
                unsorted.Add(newVal);
                unsortedList.Add(newVal);
                unsortedThreadedList[i] = newVal;

                //Uncomment to see unsorted numbers
                // Console.Write(unsorted[i] + " ");
            }

            Console.WriteLine("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

            stopWatch1.Start();
            MergeSort mergesort = new MergeSort(unsorted);
            TimeSpan  ts        = stopWatch1.Elapsed;

            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            Console.WriteLine("\nRunTime For Unthreaded: " + elapsedTime + "\n");

            sorted = mergesort.Mresult;

            Console.WriteLine("Sorted array elements: ");

            //Uncomment loop to see sorted numbers
            foreach (int x in sorted)
            {
                Console.Write(x + " ");
            }

            Console.WriteLine("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

            stopWatch2.Start();
            ThreadedMergeSort threadedMergesort = new ThreadedMergeSort(unsortedThreadedList);
            TimeSpan          tsThreaded        = stopWatch2.Elapsed;

            int[] threadedSorted = new int[unsortedThreadedList.Length];
            threadedSorted = threadedMergesort.Mnums;



            string ThreadedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                tsThreaded.Hours, tsThreaded.Minutes, tsThreaded.Seconds,
                                                tsThreaded.Milliseconds / 10);

            Console.WriteLine("\nRunTime For threaded: " + ThreadedTime + "\n");

            Console.WriteLine("Threaded based Sorted array elements: ");

            //Uncomment loop to see sorted numbers
            for (int i = 0; i < threadedSorted.Length; i++)
            {
                Console.Write(threadedSorted[i] + " ");
            }

            Console.Write("\n\nApplication has finished, press any key to continue \n\n");
            Console.ReadKey();
        }