Example #1
0
        public void Should_be_fast_and_friendly()
        {
            NewId.Next();


            int limit = 1000000;

            var ids = new NewId[limit];

            Parallel.For(0, limit, x => ids[x] = NewId.Next());

            Stopwatch timer = Stopwatch.StartNew();

            Parallel.For(0, limit, x => ids[x] = NewId.Next());

            timer.Stop();

            Console.WriteLine("Generated {0} ids in {1}ms ({2}/ms)", limit, timer.ElapsedMilliseconds,
                              limit / timer.ElapsedMilliseconds);

            Console.WriteLine("Distinct: {0}", ids.Distinct().Count());

            var duplicates = ids.GroupBy(x => x).Where(x => x.Count() > 1).ToArray();

            Console.WriteLine("Duplicates: {0}", duplicates.Count());

            foreach (var newId in duplicates)
            {
                Console.WriteLine("{0} {1}", newId.Key, newId.Count());
            }
        }
Example #2
0
        public void Should_be_completely_thread_safe_to_avoid_duplicates()
        {
            NewId.Next();

            Stopwatch timer = Stopwatch.StartNew();

            int threadCount = 20;

            var loopCount = 1024 * 1024;

            int limit = loopCount * threadCount;

            var ids = new NewId[limit];

            ParallelEnumerable
            .Range(0, limit)
            .WithDegreeOfParallelism(8)
            .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
            .ForAll(x => { ids[x] = NewId.Next(); });

            timer.Stop();

            Console.WriteLine("Generated {0} ids in {1}ms ({2}/ms)", limit, timer.ElapsedMilliseconds,
                              limit / timer.ElapsedMilliseconds);

            Console.WriteLine("Distinct: {0}", ids.Distinct().Count());

            var duplicates = ids.GroupBy(x => x).Where(x => x.Count() > 1).ToArray();

            Console.WriteLine("Duplicates: {0}", duplicates.Count());

            foreach (var newId in duplicates)
            {
                Console.WriteLine("{0} {1}", newId.Key, newId.Count());
            }
        }
Example #3
0
        public void Should_be_completely_thread_safe_to_avoid_duplicates()
        {
            NewId.Next();

            Stopwatch timer = Stopwatch.StartNew();

            int threadCount = 20;

            int workerThreads, complete;

            ThreadPool.GetMinThreads(out workerThreads, out complete);
            ThreadPool.SetMinThreads(workerThreads + threadCount, complete);


            var loopCount = 1024 * 1024;

            int limit = loopCount * threadCount;

            var ids = new NewId[limit];

            var tasks = new List <Task>();

            var begin = new TaskCompletionSource <bool>();

            for (int threadId = 0; threadId < threadCount; threadId++)
            {
                var start = threadId * loopCount;
                var end   = start + loopCount;

                var task = Task.Factory.StartNew(() =>
                {
                    //begin.Task.Wait();

                    for (int i = start; i < end; i++)
                    {
                        ids[i] = NewId.Next();
                    }
                });

                tasks.Add(task);
            }

            //begin.SetResult(true);

            Task.WaitAll(tasks.ToArray());

            timer.Stop();

            Console.WriteLine("Generated {0} ids in {1}ms ({2}/ms)", limit, timer.ElapsedMilliseconds,
                              limit / timer.ElapsedMilliseconds);

            Console.WriteLine("Distinct: {0}", ids.Distinct().Count());

            var duplicates = ids.GroupBy(x => x).Where(x => x.Count() > 1).ToArray();

            Console.WriteLine("Duplicates: {0}", duplicates.Count());

            foreach (var newId in duplicates)
            {
                Console.WriteLine("{0} {1}", newId.Key, newId.Count());
            }
        }
Example #4
0
        public void Should_be_completely_thread_safe_to_avoid_duplicates()
        {
            NewId.Next();

            Stopwatch timer = Stopwatch.StartNew();

            int threadCount = 20;

            int workerThreads, complete;
            ThreadPool.GetMinThreads(out workerThreads, out complete);
            ThreadPool.SetMinThreads(workerThreads + threadCount, complete);

            var loopCount = 1024 * 1024;

            int limit = loopCount * threadCount;

            var ids = new NewId[limit];

            var tasks = new List<Task>();

            var begin = new TaskCompletionSource<bool>();

            for (int threadId = 0; threadId < threadCount; threadId++)
            {
                var start = threadId * loopCount;
                var end = start + loopCount;

                var task = Task.Factory.StartNew(() =>
                {
                    //begin.Task.Wait();

                    for (int i = start; i < end; i++)
                        ids[i] = NewId.Next();
                });

                tasks.Add(task);
            }

            //begin.SetResult(true);

            Task.WaitAll(tasks.ToArray());

            timer.Stop();

            Console.WriteLine("Generated {0} ids in {1}ms ({2}/ms)", limit, timer.ElapsedMilliseconds,
                limit / timer.ElapsedMilliseconds);

            Console.WriteLine("Distinct: {0}", ids.Distinct().Count());

            var duplicates = ids.GroupBy(x => x).Where(x => x.Count() > 1).ToArray();

            Console.WriteLine("Duplicates: {0}", duplicates.Count());

            foreach (var newId in duplicates)
            {
                Console.WriteLine("{0} {1}", newId.Key, newId.Count());
            }
        }