Exemplo n.º 1
0
        // https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx
        private static int ParallelTaskLoop(List <long> names, RedBlackSearchTree strTree)
        {
            int countCPU = 8;

            Task <int>[] tasks = new Task <int> [countCPU];
            for (var j = 0; j < countCPU; j++)
            {
                tasks[j] = Task <int> .Factory.StartNew(
                    (object p) =>
                {
                    int count = 0;
                    for (int i = (int)p; i < names.Count; i += countCPU)
                    {
                        if (strTree.Contains(names[i].ToString()))
                        {
                            count++;
                        }
                    }
                    return(count);
                }, j);
            }
            int total = 0;

            for (var i = 0; i < countCPU; i++)
            {
                total += tasks[i].Result;
            }
            return(total);
        }
Exemplo n.º 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 = 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);
        }
Exemplo n.º 3
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);
             */
        }
Exemplo n.º 4
0
        private static int SequentialLoop(List <long> names, RedBlackSearchTree strTree)
        {
            int count = 0;

            for (int i = 0; i < names.Count; i++)
            {
                if (strTree.Contains(names[i].ToString()))
                {
                    count++;
                }
            }
            return(count);
        }