示例#1
0
        static void Main(string[] args)
        {
            List <long>        nameList = new List <long>();
            RedBlackSearchTree strTree  = new RedBlackSearchTree();
            int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;

            random = new Random(seed);
            int n = 15;

            for (int i = 0; i < n; i++)
            {
                long s = random.Next(20000);
                nameList.Add(s);
                strTree.Insert(s);
            }
            nameList.Add(random.Next(20000));
            Console.WriteLine(" Binary Search Tree \n");
            strTree.Print();
            Console.WriteLine("\n Search Test \n");
            long allTime = 0; //all execution time

            foreach (long s in nameList)
            {
                var time = new System.Diagnostics.Stopwatch();
                time.Start();
                bool contains = strTree.Contains(s.ToString());
                //strTree.Contains(s);
                time.Stop();
                allTime += time.ElapsedTicks;
                Console.Write(contains.ToString() + " " + time.ElapsedTicks + "ticks" + " ");
            }
            Console.WriteLine("\n");
            Console.WriteLine("{0}ms", (double)allTime / 10000);
        }
示例#2
0
        static void Main(string[] args)
        {
            List <long>        nameList = new List <long>();
            RedBlackSearchTree strTree  = new RedBlackSearchTree();
            int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;

            random = new Random(seed);

            int n         = 50000;
            var stopWatch = new Stopwatch();

            for (int i = 0; i < n; i++)
            {
                long s = random.Next(20000);
                nameList.Add(s);
                strTree.Insert(s);
            }


            nameList.Add(random.Next(20000));

            stopWatch.Start();
            int count = SequentialLoop(nameList, strTree);

            stopWatch.Stop();
            Console.WriteLine("Time in milliseconds for sequential loop: {0,6:N0} ",
                              stopWatch.ElapsedMilliseconds);
            Console.WriteLine("Contains: {0,6:N0} Total: {1,6:N0}", count, nameList.Count);

            stopWatch.Reset();
            stopWatch.Start();

            count = ParallelTaskLoop(nameList, strTree);
            stopWatch.Stop();
            Console.WriteLine("Time in milliseconds for parallel loop: {0,6:N0} ",
                              stopWatch.ElapsedMilliseconds);
            Console.WriteLine("Contains: {0,6:N0} Total: {1,6:N0}", count, nameList.Count);


            /*
             * Console.WriteLine(" Binary Search Tree \n");
             * strTree.Print();
             * Console.WriteLine("\n Search Test \n");
             * long allTime = 0; //all execution time
             * foreach (long s in nameList)
             * {
             *  var time = new System.Diagnostics.Stopwatch();
             *  time.Start();
             *  bool contains = strTree.Contains(s.ToString());
             *  //strTree.Contains(s);
             *  time.Stop();
             *  allTime += time.ElapsedTicks;
             *  Console.Write(contains.ToString() + " "+ time.ElapsedTicks + "ticks" + " ");
             * }
             * Console.WriteLine("\n");
             * Console.WriteLine("{0}ms", (double)allTime / 10000);
             */
        }