示例#1
0
        // Generates/returns an array of given size with randomly generated MobileObjects
        public static MobileObject[] GenerateMobileObjects(int size)
        {
            Random rnd = new Random();

            string[] names = new string[10] {
                "Toast", "Toasty", "Toaster", "Cactus", "Notacactus", "Pancreas", "ToastMaster", "ToastApprentice", "NotToast", "Bread"
            };
            MobileObject[] mObjects = new MobileObject[size];
            for (int i = 0; i < size; i++)
            {
                if (i % 2 == 1)
                {
                    mObjects[i] = new NPC(rnd, names[i % names.Length], i, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99);
                }
                else
                {
                    mObjects[i] = new Vehicle(rnd, names[i % names.Length], i, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99);
                }
            }
            return(mObjects);
        }
示例#2
0
        // Times inserting given number of objects into each tree, performs given number of trial and outputs average runtimes in MS
        public static void TimingTest(int numElements, int numTrials)
        {
            MobileObject[] mObjects      = GenerateMobileObjects(numElements);
            MobileObject[] sortedObjects = new MobileObject[mObjects.Length];
            mObjects.CopyTo(sortedObjects, 0);
            Array.Sort(sortedObjects);
            long elapsedTicBST            = 0;
            long elapsedTicBSTSorted      = 0;
            long elapsedTicAVL            = 0;
            long elapsedTicAVLSorted      = 0;
            long elapsedTicRedBlack       = 0;
            long elapsedTicRedBlackSorted = 0;
            long elapsedTicSplay          = 0;
            long elapsedTicSplaySorted    = 0;
            long elapsedTic234            = 0;
            long elapsedTic234Sorted      = 0;

            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            Console.Write("Timing insertion of " + numElements + " elements(sorted and unsorted)");
            for (int i = 1; i <= numTrials; i++)
            {
                BinarySearchTree <MobileObject> binarySearchTree  = new BinarySearchTree <MobileObject>();
                BinarySearchTree <MobileObject> binarySearchTree2 = new BinarySearchTree <MobileObject>();
                AVLTree <MobileObject>          avlTree           = new AVLTree <MobileObject>();
                AVLTree <MobileObject>          avlTree2          = new AVLTree <MobileObject>();
                RedBlackTree <MobileObject>     redBlackTree      = new RedBlackTree <MobileObject>();
                RedBlackTree <MobileObject>     redBlackTree2     = new RedBlackTree <MobileObject>();
                SplayTree <MobileObject>        splayTree         = new SplayTree <MobileObject>();
                SplayTree <MobileObject>        splayTree2        = new SplayTree <MobileObject>();
                BTree234 <MobileObject>         _234Tree          = new BTree234 <MobileObject>();
                BTree234 <MobileObject>         _234Tree2         = new BTree234 <MobileObject>();
                Console.Write("\nTrial " + i + ":");
                var watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    binarySearchTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicBST += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    binarySearchTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicBSTSorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    avlTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicAVL += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    avlTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicAVLSorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    redBlackTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicRedBlack += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    redBlackTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicRedBlackSorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    splayTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicSplay += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    splayTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicSplaySorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    _234Tree.Insert(obj);
                }
                watch.Stop();
                elapsedTic234 += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    _234Tree2.Insert(obj);
                }
                watch.Stop();
                elapsedTic234Sorted += watch.ElapsedTicks;
                Console.Write(" .");
            }
            elapsedTicBST            /= numTrials;
            elapsedTicBSTSorted      /= numTrials;
            elapsedTicAVL            /= numTrials;
            elapsedTicAVLSorted      /= numTrials;
            elapsedTicRedBlack       /= numTrials;
            elapsedTicRedBlackSorted /= numTrials;
            elapsedTicSplay          /= numTrials;
            elapsedTicSplaySorted    /= numTrials;
            elapsedTic234            /= numTrials;
            elapsedTic234Sorted      /= numTrials;

            Console.WriteLine("\n{0, -55}" + elapsedTicBST + " MS"
                              + "\n{1, -55}" + elapsedTicBSTSorted + " MS"
                              + "\n{2, -55}" + elapsedTicAVL + " MS"
                              + "\n{3, -55}" + elapsedTicAVLSorted + " MS"
                              + "\n{4, -55}" + elapsedTicRedBlack + " MS"
                              + "\n{5, -55}" + elapsedTicRedBlackSorted + " MS"
                              + "\n{6, -55}" + elapsedTicSplay + " MS"
                              + "\n{7, -55}" + elapsedTicSplaySorted + " MS"
                              + "\n{8, -55}" + elapsedTic234 + " MS"
                              + "\n{9, -55}" + elapsedTic234Sorted + " MS"
                              , "BinarySearchTree Insert " + numElements + " MobileObjects:"
                              , "BinarySearchTree Insert " + numElements + " MobileObjects(sorted):"
                              , "AVLTree Insert " + numElements + " MobileObjects:"
                              , "AVLTree Insert " + numElements + " MobileObjects (sorted):"
                              , "RedBlackTree Insert " + numElements + " MobileObjects:"
                              , "RedBlackTree Insert " + numElements + " MobileObjects (sorted):"
                              , "SplayTree Insert " + numElements + " MobileObjects:"
                              , "SplayTree Insert " + numElements + " MobileObjects (sorted):"
                              , "234Tree Insert " + numElements + " MobileObjects:"
                              , "234LTree Insert " + numElements + " MobileObjects (sorted):");
        }