Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            var arraySize = 50000000;
            var array     = BuildAnArray(arraySize);

            var stopwatch  = Stopwatch.StartNew();
            var firstArray = array.Take(array.Length / 2);
            var lastArray  = array.Skip(array.Length / 2);

            var arrayProcessorFirst = new ArrayProcessor(firstArray.ToArray(), 0, firstArray.ToList().Count);
            var arrayProcessorLast  = new ArrayProcessor(lastArray.ToArray(), 0, lastArray.ToList().Count);

            Thread firstThread = new Thread(arrayProcessorFirst.CalculateSum);
            Thread lastThread  = new Thread(arrayProcessorLast.CalculateSum);

            //var arrayProcessor = new ArrayProcessor(array, 0, arraySize);
            //arrayProcessor.CalculateSum();
            firstThread.Start();
            lastThread.Start();


            lastThread.Join();
            firstThread.Join();


            //var totalSum = arrayProcessor.Sum;
            var totalSum = arrayProcessorFirst.Sum + arrayProcessorLast.Sum;

            stopwatch.Stop();

            Console.WriteLine($"Elapsed time: {stopwatch.Elapsed.TotalMilliseconds} ms");
            Console.WriteLine($"Sum: {totalSum}");
        }
Exemplo n.º 2
0
        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}");
        }