Facebook
From Cukrowicz, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 291
  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.  
  28.             int sum = 0;
  29.             int range = Index + ToSum;
  30.            
  31.  
  32.             try
  33.             {
  34.                 for (int i = Index; i < range; i++)
  35.                 {
  36.                     sum += randomList[i];
  37.                 }
  38.             }
  39.             catch(IndexOutOfRangeException)
  40.             {
  41.             }
  42.             sumValue += sum;
  43.  
  44.         }
  45.         static List<int> SetChunk(int AmmontOfThreads)
  46.         {
  47.             if (AmmontOfThreads == 0)
  48.                 throw new Exception("Ammount of Threads cannot be zero");
  49.  
  50.             List<int> chunks = new List<int>();
  51.             int toCover = randomList.Count;
  52.             double ch = randomList.Count / AmmontOfThreads;
  53.  
  54.             for (int i = 0; i < AmmontOfThreads; i++)
  55.             {
  56.                 if (i == AmmontOfThreads - 1)
  57.                 {
  58.                     chunks.Add(toCover);
  59.                 }
  60.                 else
  61.                 {
  62.                     int p = (int)Math.Floor(ch);
  63.                     chunks.Add(p);
  64.                     toCover -= p;
  65.                 }
  66.                    
  67.             }
  68.             return chunks;
  69.         }
  70.  
  71.         static void Main(string[] args)
  72.         {
  73.             StreamWriter file = new StreamWriter("ParallelTime.txt");
  74.             int numberOfThreads = 1;
  75.             var parallelStopwatch = new Stopwatch();
  76.             List<Thread> threadList = new List<Thread>();
  77.             List<int> listOfChunks = new List<int>();
  78.             List<int> listOfIndex = new List<int>();
  79.             List<long> ticks = new List<long>();
  80.             int n = 10000000;
  81.             FillList(n);
  82.  
  83.             while (numberOfThreads!=200)
  84.             {
  85.                 int tries = 0;
  86.                 while (tries<20)
  87.                 {
  88.                     FillList(n);
  89.                     listOfChunks = SetChunk(numberOfThreads);
  90.                     int currentIndex = 0;
  91.  
  92.                     foreach (int chunk in listOfChunks)
  93.                     {
  94.                         listOfIndex.Add(currentIndex);
  95.                         currentIndex += chunk;
  96.                     }
  97.  
  98.                     parallelStopwatch.Restart();
  99.                     for (int i = 0; i < listOfIndex.Count; i++)
  100.                     {
  101.                         int h = i;//z jakiegos powodu trzeba stworzyc zmienna lokalna dla nowego watku, w przypadku uzywania i crashuje sie
  102.                         var t = new Thread(() => PartialSum(listOfIndex[h], listOfChunks[h]));
  103.                         t.Start();
  104.                         threadList.Add(t);
  105.                     }
  106.                     foreach (var t in threadList)
  107.                         t.Join();
  108.  
  109.                     parallelStopwatch.Stop();
  110.                     ticks.Add(parallelStopwatch.ElapsedTicks);
  111.  
  112.                     listOfChunks.Clear();
  113.                     listOfIndex.Clear();
  114.                     threadList.Clear();
  115.                     randomList.Clear();
  116.                     tries++;
  117.                 }
  118.                 ticks.Sort();
  119.                 double avg = ticks[(ticks.Count - 1) / 2];
  120.                 double time = (avg / Stopwatch.Frequency) * 1000;
  121.                 file.WriteLine(numberOfThreads + ";" + time);
  122.  
  123.                 ticks.Clear();
  124.                 numberOfThreads++;
  125.             }
  126.             file.Dispose();
  127.         }  
  128.     }
  129. }
  130.