Esempio n. 1
0
        public static void Main(string[] args)
        {
            var arraySize = 50000000; // 50 000 000
            var array     = BuildAnArray(arraySize);

            int        nrThreads   = 4;
            BigInteger totalSum    = 0;
            int        dataSplitAt = arraySize / nrThreads;

            _countdown = new CountdownEvent(nrThreads);

            var stopwatch = Stopwatch.StartNew();

            ArrayProcessor[] arrayProcessors = new ArrayProcessor[nrThreads];
            for (int i = 0; i < nrThreads; i++)
            {
                arrayProcessors[i] = new ArrayProcessor(array, dataSplitAt * i, dataSplitAt);
                new Thread(arrayProcessors[i].CalculateSum).Start();
            }

            _countdown.Wait();   // Blocks until Signal has been called nrThreads times

            for (int i = 0; i < nrThreads; i++)
            {
                totalSum += arrayProcessors[i].Sum;
            }


            stopwatch.Stop();

            Console.WriteLine($"Elapsed time: {stopwatch.Elapsed.TotalMilliseconds} ms");
            Console.WriteLine($"Sum: {totalSum}");
        }
        private static void Main(string[] args)
        {
            var arraySize = 50000000; // 50 000 000
            var array     = BuildAnArray(arraySize);

            var stopwatch = Stopwatch.StartNew();

            var arrayProcessor = new ArrayProcessor(array, 0, arraySize);

            arrayProcessor.CalculateSum();
            var totalSum = arrayProcessor.Sum;

            stopwatch.Stop();

            Console.WriteLine($"Elapsed time: {stopwatch.Elapsed.TotalMilliseconds} ms");
            Console.WriteLine($"Sum: {totalSum}");
        }