Esempio n. 1
0
        private bool CheckCollisions()
        {
            BigInteger[] numbers = new BigInteger[CollisionChecks];
            Task[]       tasks   = new Task[Environment.ProcessorCount];

            var options = new ProgressBarOptions
            {
                ProgressBarOnBottom   = true,
                ShowEstimatedDuration = true
            };


            using (var pb = new ProgressBar((int)CollisionChecks, "Checking for collisions", options))
            {
                //Launch tasks to fill each item
                for (int i = 0; i < Environment.ProcessorCount; i++)
                {
                    int id = i + 0;
                    tasks[i] = Task.Run(() => PopulateArray(ref numbers, pb, id));
                    System.Threading.Thread.Sleep(150);
                }


                //Wait for fill
                foreach (var t in tasks)
                {
                    t.Wait();
                }
            }

            var collisions = CollisionChecks - numbers.AsParallel().Distinct().Count();

            Console.WriteLine($"There were {collisions} collisions\t({collisions / (float)CollisionChecks * 100}%)");

            //If all are unique return true
            return(collisions == 0);
        }