Facebook
From Cukrowicz, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 271
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace ConsoleApp1
  11. {
  12.     class Program
  13.     {
  14.         static Int64 sumValue = 0;
  15.         static List<int> randomList = new List<int>();
  16.  
  17.         static void FillList(int Length)
  18.         {
  19.             var rand = new Random();
  20.             for(int i=0;i<Length;i++)
  21.             {
  22.                 randomList.Add(rand.Next(1, 5000));
  23.             }
  24.         }
  25.         static void PartialSum(int Index, int ToSum)
  26.         {
  27.             int sum = 0;
  28.             int range = Index + ToSum;
  29.            
  30.             try
  31.             {
  32.                 for (int i = Index; i < range; i++)
  33.                 {
  34.                     sum += randomList[i];
  35.                 }
  36.             }
  37.             catch(IndexOutOfRangeException)
  38.             {
  39.             }
  40.             sumValue += sum;
  41.  
  42.         }
  43.         static List<int> SetChunk(int AmmontOfThreads)
  44.         {
  45.             if (AmmontOfThreads == 0)
  46.                 throw new Exception("Ammount of Threads cannot be zero");
  47.  
  48.             List<int> chunks = new List<int>();
  49.             int toCover = randomList.Count;
  50.             double ch = randomList.Count / AmmontOfThreads;
  51.  
  52.             for (int i = 0; i < AmmontOfThreads; i++)
  53.             {
  54.                 if (i == AmmontOfThreads - 1)
  55.                 {
  56.                     chunks.Add(toCover);
  57.                 }
  58.                 else
  59.                 {
  60.                     int p = (int)Math.Floor(ch);
  61.                     chunks.Add(p);
  62.                     toCover -= p;
  63.                 }                  
  64.             }
  65.             return chunks;
  66.         }
  67.  
  68.         static void Main(string[] args)
  69.         {
  70.             StreamWriter pFile = new StreamWriter("ParallelTime.txt");
  71.             StreamWriter sFile = new StreamWriter("SeqTime.txt");
  72.             int numbersToSum = 1;
  73.             var parallelStopwatch = new Stopwatch();
  74.             List<Thread> threadList = new List<Thread>();
  75.             List<int> listOfChunks = new List<int>();
  76.             List<int> listOfIndex = new List<int>();
  77.             List<long> ticks = new List<long>();
  78.             int n = 1000000;
  79.             FillList(n);
  80.  
  81.             while (numbersToSum<n)
  82.             {
  83.                 int tries = 0;
  84.                 while (tries<20)
  85.                 {
  86.                     FillList(numbersToSum);
  87.                     listOfChunks = SetChunk(10);
  88.                     int currentIndex = 0;
  89.  
  90.                     foreach (int chunk in listOfChunks)
  91.                     {
  92.                         listOfIndex.Add(currentIndex);
  93.                         currentIndex += chunk;
  94.                     }
  95.  
  96.                     parallelStopwatch.Restart();
  97.                     for (int i = 0; i < listOfIndex.Count; i++)
  98.                     {
  99.                         int h = i;//z jakiegos powodu trzeba stworzyc zmienna lokalna dla nowego watku, w przypadku uzywania i crashuje sie
  100.                         var t = new Thread(() => PartialSum(listOfIndex[h], listOfChunks[h]));
  101.                         t.Start();
  102.                         threadList.Add(t);
  103.                     }
  104.                     foreach (var t in threadList)
  105.                         t.Join();
  106.  
  107.                     parallelStopwatch.Stop();
  108.                     ticks.Add(parallelStopwatch.ElapsedTicks);
  109.  
  110.                     listOfChunks.Clear();
  111.                     listOfIndex.Clear();
  112.                     threadList.Clear();
  113.                     randomList.Clear();
  114.                     tries++;
  115.                 }
  116.                 ticks.Sort();
  117.                 double avg = ticks[(ticks.Count - 1) / 2];
  118.                 double time = (avg / Stopwatch.Frequency) * 1000;
  119.                 pFile.WriteLine(numbersToSum + ";" + time);
  120.                 ticks.Clear();
  121.  
  122.                 Int64 agg = 0;
  123.                 Stopwatch seq = new Stopwatch();
  124.                 for(int i=0;i<20;i++)
  125.                 {
  126.                     seq.Restart();
  127.                     FillList(numbersToSum);
  128.                     foreach (var number in randomList)
  129.                         agg += number;
  130.                     seq.Stop();
  131.  
  132.                     randomList.Clear();
  133.                     ticks.Add(seq.ElapsedTicks);                
  134.                 }
  135.                 ticks.Sort();
  136.                 avg = ticks[(ticks.Count - 1) / 2];
  137.                 time = (avg / Stopwatch.Frequency) * 1000;
  138.                 sFile.WriteLine(numbersToSum + ";" + time);
  139.  
  140.                 if (numbersToSum >= 100000)
  141.                     numbersToSum += 1000;
  142.                  if (numbersToSum > 5000 && numbersToSum < 100000)
  143.                     numbersToSum += 100;
  144.                  if (numbersToSum <= 5000)
  145.                     numbersToSum += 10;
  146.                 Console.Clear();
  147.                 Console.WriteLine("{0} out of {1}",numbersToSum,n);
  148.                
  149.             }
  150.             pFile.Dispose();
  151.             sFile.Dispose();
  152.         }  
  153.     }
  154. }
  155.