- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static Int64 sumValue = 0;
- static List<int> randomList = new List<int>();
- static void FillList(int Length)
- {
- var rand = new Random();
- for(int i=0;i<Length;i++)
- {
- randomList.Add(rand.Next(1, 5000));
- }
- }
- static void PartialSum(int Index, int ToSum)
- {
- int sum = 0;
- int range = Index + ToSum;
- try
- {
- for (int i = Index; i < range; i++)
- {
- sum += randomList[i];
- }
- }
- catch(IndexOutOfRangeException)
- {
- }
- sumValue += sum;
- }
- static List<int> SetChunk(int AmmontOfThreads)
- {
- if (AmmontOfThreads == 0)
- throw new Exception("Ammount of Threads cannot be zero");
- List<int> chunks = new List<int>();
- int toCover = randomList.Count;
- double ch = randomList.Count / AmmontOfThreads;
- for (int i = 0; i < AmmontOfThreads; i++)
- {
- if (i == AmmontOfThreads - 1)
- {
- chunks.Add(toCover);
- }
- else
- {
- int p = (int)Math.Floor(ch);
- chunks.Add(p);
- toCover -= p;
- }
- }
- return chunks;
- }
- static void Main(string[] args)
- {
- StreamWriter pFile = new StreamWriter("ParallelTime.txt");
- StreamWriter sFile = new StreamWriter("SeqTime.txt");
- int numbersToSum = 1;
- var parallelStopwatch = new Stopwatch();
- List<Thread> threadList = new List<Thread>();
- List<int> listOfChunks = new List<int>();
- List<int> listOfIndex = new List<int>();
- List<long> ticks = new List<long>();
- int n = 1000000;
- FillList(n);
- while (numbersToSum<n)
- {
- int tries = 0;
- while (tries<20)
- {
- FillList(numbersToSum);
- listOfChunks = SetChunk(10);
- int currentIndex = 0;
- foreach (int chunk in listOfChunks)
- {
- listOfIndex.Add(currentIndex);
- currentIndex += chunk;
- }
- parallelStopwatch.Restart();
- for (int i = 0; i < listOfIndex.Count; i++)
- {
- int h = i;//z jakiegos powodu trzeba stworzyc zmienna lokalna dla nowego watku, w przypadku uzywania i crashuje sie
- var t = new Thread(() => PartialSum(listOfIndex[h], listOfChunks[h]));
- t.Start();
- threadList.Add(t);
- }
- foreach (var t in threadList)
- t.Join();
- parallelStopwatch.Stop();
- ticks.Add(parallelStopwatch.ElapsedTicks);
- listOfChunks.Clear();
- listOfIndex.Clear();
- threadList.Clear();
- randomList.Clear();
- tries++;
- }
- ticks.Sort();
- double avg = ticks[(ticks.Count - 1) / 2];
- double time = (avg / Stopwatch.Frequency) * 1000;
- pFile.WriteLine(numbersToSum + ";" + time);
- ticks.Clear();
- Int64 agg = 0;
- Stopwatch seq = new Stopwatch();
- for(int i=0;i<20;i++)
- {
- seq.Restart();
- FillList(numbersToSum);
- foreach (var number in randomList)
- agg += number;
- seq.Stop();
- randomList.Clear();
- ticks.Add(seq.ElapsedTicks);
- }
- ticks.Sort();
- avg = ticks[(ticks.Count - 1) / 2];
- time = (avg / Stopwatch.Frequency) * 1000;
- sFile.WriteLine(numbersToSum + ";" + time);
- if (numbersToSum >= 100000)
- numbersToSum += 1000;
- if (numbersToSum > 5000 && numbersToSum < 100000)
- numbersToSum += 100;
- if (numbersToSum <= 5000)
- numbersToSum += 10;
- Console.Clear();
- Console.WriteLine("{0} out of {1}",numbersToSum,n);
- }
- pFile.Dispose();
- sFile.Dispose();
- }
- }
- }