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 randomList = new List(); static void FillList(int Length) { var rand = new Random(); for(int i=0;i SetChunk(int AmmontOfThreads) { if (AmmontOfThreads == 0) throw new Exception("Ammount of Threads cannot be zero"); List chunks = new List(); 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 threadList = new List(); List listOfChunks = new List(); List listOfIndex = new List(); List ticks = new List(); int n = 1000000; FillList(n); while (numbersToSum 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(); } } }