GreenMap

Лабораторная 2 по ААС

Mar 5th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Lab_2_AAC
  10. {
  11.     class Program
  12.     {
  13.         public static int[] n = { 1000, 10000, 100000 }; //колличество элементов должно быть = колличеству эл. в thread_count_for_1
  14.         public static List<List<int>> first_simple_numbers_list = new List<List<int>>();
  15.         public static List<int> second_simple_numbers_m1 = new List<int>();
  16.         public static List<int> second_simple_numbers_m2 = new List<int>();
  17.         public static List<int> second_simple_numbers_m3 = new List<int>();
  18.         public static List<int> second_simple_numbers_m4 = new List<int>();
  19.         public static int _number_not_finished_threads_m3;
  20.         public static ManualResetEvent finish_work_event = new ManualResetEvent(false);
  21.         public static List<int> remove_numbers_m2 = new List<int>();
  22.         public static List<int> remove_numbers_m3 = new List<int>();
  23.         public static List<int> remove_numbers_m4 = new List<int>();
  24.         public static List<int> cheked_simple_m4 = new List<int>();
  25.         public static double[] avarage_performance_time = new double[4];
  26.         public static object lockObj = new object();
  27.         static int[] thread_count_for_1 = { 1, 5, 10 };
  28.         static void Main(string[] args)
  29.         {
  30.             for (int i = 0; i < n.Length; i++)
  31.             {
  32.                 first_simple_numbers_list.Add(new List<int>());
  33.                 first_simple_numbers_list[i].Add(2);
  34.             }
  35.             for (int list_num = 0; list_num < first_simple_numbers_list.Count; list_num++)
  36.             {
  37.                 for (int i = 2; i < Convert.ToInt32(Math.Sqrt(n[list_num])); i++)
  38.                 {
  39.                     bool simple = true;
  40.                     foreach (var j in first_simple_numbers_list[list_num])
  41.                     {
  42.                         if (i % j == 0)
  43.                         {
  44.                             simple = false;
  45.                         }
  46.                     }
  47.                     if (simple)
  48.                     {
  49.                         first_simple_numbers_list[list_num].Add(i);
  50.                     }
  51.                 }
  52.             }
  53.             if (first_simple_numbers_list[0].Count < thread_count_for_1[thread_count_for_1.Length - 1])
  54.             {
  55.                 Console.WriteLine("Потоков больше чем простых чисел!");
  56.             }//проверка на ошибки
  57.             List_Printer(first_simple_numbers_list[0]);
  58.             //method 1
  59.             int all_time = 0;
  60.             for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
  61.             {
  62.                 foreach (var thread_count in thread_count_for_1)
  63.                 {
  64.                     second_simple_numbers_m1.Clear();
  65.                     Stopwatch time = new Stopwatch();
  66.                     time.Start();
  67.                     Thread[] threads = new Thread[thread_count];
  68.                     for (int i = 0; i < thread_count; i++)
  69.                     {
  70.                         Parametrs_for_thread param = new Parametrs_for_thread();
  71.                         param.thread_num = i;
  72.                         param.thread_count = thread_count;
  73.                         param.elements_count_num = elements_count_num;
  74.                         threads[i] = new Thread(Work_m1);
  75.                         threads[i].Start(param);
  76.                     }
  77.                     for (int i = 0; i < thread_count; i++)
  78.                     {
  79.                         threads[i].Join();
  80.                     }
  81.                     time.Stop();
  82.                     Console.WriteLine("Время обработки с помощью первого метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
  83.                     all_time += (int)time.ElapsedMilliseconds;
  84.                 }
  85.                 //second_simple_numbers_m1 - ответ
  86.             }
  87.             avarage_performance_time[0] = all_time / (n.Length * thread_count_for_1.Length);
  88.             // method 2
  89.             all_time = 0;
  90.             for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
  91.             {
  92.                 foreach (var thread_count in thread_count_for_1)
  93.                 {
  94.                     second_simple_numbers_m2.Clear();
  95.                     Stopwatch time = new Stopwatch();
  96.                     time.Start();
  97.                     for(int i = Convert.ToInt32(Math.Sqrt(n[elements_count_num])); i < n[elements_count_num]; i++)
  98.                     {
  99.                         second_simple_numbers_m2.Add(i);
  100.                     }//заполняем список значениями от корня из н до н чтобы потом удалять ненужные
  101.                     Thread[] threads = new Thread[thread_count];
  102.                     for (int i = 0; i < thread_count; i++)
  103.                     {
  104.                         Parametrs_for_thread param = new Parametrs_for_thread();
  105.                         param.thread_num = i;
  106.                         param.thread_count = thread_count;
  107.                         param.elements_count_num = elements_count_num;
  108.                         threads[i] = new Thread(Work_m2);
  109.                         threads[i].Start(param);
  110.                     }
  111.                     for (int i = 0; i < thread_count; i++)
  112.                     {
  113.                         threads[i].Join();
  114.                     }
  115.                     foreach (var number in remove_numbers_m2)
  116.                     {
  117.                         second_simple_numbers_m2.Remove(number);
  118.                     }
  119.                     time.Stop();
  120.                     Console.WriteLine("Время обработки с помощью второго метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
  121.                     all_time += (int)time.ElapsedMilliseconds;
  122.                     //second_simple_numbers_m2 - ответ
  123.                 }
  124.             }
  125.             avarage_performance_time[1] = all_time / (double)(n.Length * thread_count_for_1.Length);
  126.             // method 3
  127.             all_time = 0;
  128.             for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
  129.             {
  130.                 foreach (var thread_count in thread_count_for_1)
  131.                 {
  132.                     second_simple_numbers_m3.Clear();
  133.                     Stopwatch time = new Stopwatch();
  134.                     time.Start();
  135.                     for (int i = Convert.ToInt32(Math.Sqrt(n[elements_count_num])); i < n[elements_count_num]; i++)
  136.                     {
  137.                         second_simple_numbers_m3.Add(i);
  138.                     }//заполняем список значениями от корня из н до н чтобы потом удалять ненужные
  139.                     _number_not_finished_threads_m3 = first_simple_numbers_list[elements_count_num].Count;
  140.                     ThreadPool.SetMaxThreads(thread_count, thread_count);
  141.                     for(int simple_number = 0; simple_number < first_simple_numbers_list[elements_count_num].Count; simple_number++)
  142.                     {
  143.                         ThreadPool.QueueUserWorkItem(new WaitCallback(Work_m3), new object[] { first_simple_numbers_list[elements_count_num][simple_number]});
  144.                     }
  145.                     finish_work_event.WaitOne();
  146.                     foreach(var number in remove_numbers_m3)
  147.                     {
  148.                         second_simple_numbers_m3.Remove(number);
  149.                     }
  150.                     finish_work_event = new ManualResetEvent(false);
  151.                     time.Stop();
  152.                     Console.WriteLine("Время обработки с помощью третьего метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
  153.                     all_time += (int)time.ElapsedMilliseconds;
  154.  
  155.                 }
  156.             }
  157.             avarage_performance_time[2] = all_time / (n.Length * thread_count_for_1.Length);
  158.             // method 4
  159.             all_time = 0;
  160.             for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
  161.             {
  162.                 foreach (var thread_count in thread_count_for_1)
  163.                 {
  164.                     second_simple_numbers_m4.Clear();
  165.                     Stopwatch time = new Stopwatch();
  166.                     time.Start();
  167.                     for (int i = Convert.ToInt32(Math.Sqrt(n[elements_count_num])); i < n[elements_count_num]; i++)
  168.                     {
  169.                         second_simple_numbers_m4.Add(i);
  170.                     }//заполняем список значениями от корня из н до н чтобы потом удалять ненужные
  171.                     Thread[] threads = new Thread[thread_count];
  172.                     for (int i = 0; i < thread_count; i++)
  173.                     {
  174.                         Parametrs_for_thread param = new Parametrs_for_thread();
  175.                         param.thread_num = i;
  176.                         param.thread_count = thread_count;
  177.                         param.elements_count_num = elements_count_num;
  178.                         threads[i] = new Thread(Work_m4);
  179.                         threads[i].Start(param);
  180.                     }
  181.                     for (int i = 0; i < thread_count; i++)
  182.                     {
  183.                         threads[i].Join();
  184.                     }
  185.                     foreach (var number in remove_numbers_m4)
  186.                     {
  187.                         second_simple_numbers_m4.Remove(number);
  188.                     }
  189.                     time.Stop();
  190.                     Console.WriteLine("Время обработки с помощью четвертого метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
  191.                     all_time += (int)time.ElapsedMilliseconds;
  192.                     //second_simple_numbers_m4 - ответ
  193.                 }
  194.             }
  195.             avarage_performance_time[3] = all_time / (n.Length * thread_count_for_1.Length);
  196.             for(int i = 0; i < avarage_performance_time.Length; i++)
  197.             {
  198.                 Console.WriteLine("Среднее время выполнения по {0} методу: {1}", i+1, avarage_performance_time[i]);
  199.             }
  200.             Console.ReadLine();
  201.         }
  202.         static void List_Printer(List<int> list)
  203.         {
  204.             list.Sort();
  205.             foreach (var num in list)
  206.             {
  207.                 Console.Write(" {0}", num);
  208.             }
  209.             Console.WriteLine("");
  210.         }
  211.         static void Work_m1(object x)
  212.         {
  213.             Parametrs_for_thread param = (Parametrs_for_thread)x;
  214.             int n_sqrt = Convert.ToInt32(Math.Sqrt(n[param.elements_count_num]));
  215.             int elements_count = (n[param.elements_count_num] - n_sqrt) / param.thread_count;
  216.             for (int i = n_sqrt + elements_count * param.thread_num; i < n_sqrt + elements_count * (param.thread_num + 1); i++)
  217.             {
  218.                 bool simple_flag = true;
  219.                 foreach (var simple in first_simple_numbers_list[param.elements_count_num])
  220.                 {
  221.                     if (i % simple == 0)
  222.                     {
  223.                         simple_flag = false;
  224.                     }
  225.                 }
  226.                 if (simple_flag)
  227.                 {
  228.                     Program.second_simple_numbers_m1.Add(i);
  229.                 }
  230.             }
  231.  
  232.         }
  233.         static void Work_m2(object x)
  234.         {
  235.             Parametrs_for_thread param = (Parametrs_for_thread)x;
  236.             List<int> current_simple_numbers = Program.first_simple_numbers_list[param.elements_count_num];
  237.             int simple_numbers_begin_num = param.thread_num * (current_simple_numbers.Count / param.thread_count);
  238.             int simple_numbers_end_num = (param.thread_num + 1) * (current_simple_numbers.Count / param.thread_count);
  239.             for (int i = 0; i < Program.second_simple_numbers_m2.Count; i++)
  240.             {
  241.                 for (int simple_num = simple_numbers_begin_num; simple_num < simple_numbers_end_num; simple_num++)
  242.                 {
  243.                     if (Program.second_simple_numbers_m2[i] % current_simple_numbers[simple_num] == 0)
  244.                     {
  245.                         remove_numbers_m2.Add(second_simple_numbers_m2[i]);
  246.                         break;
  247.                     }
  248.                    
  249.                 }
  250.             }
  251.         }
  252.         static void Work_m3(object x)
  253.         {
  254.             int simple_number = (int)((object[])x)[0];
  255.             for (int number = 0; number < second_simple_numbers_m3.Count; number++)
  256.             {
  257.                 if(second_simple_numbers_m3[number] % simple_number == 0)
  258.                 {
  259.                     remove_numbers_m3.Add(second_simple_numbers_m3[number]);
  260.                 }
  261.             }
  262.             if(Interlocked.Decrement(ref _number_not_finished_threads_m3) == 0)
  263.             {
  264.                 finish_work_event.Set();
  265.             }
  266.            
  267.         }
  268.         static void Work_m4(object x)
  269.         {
  270.             lock (lockObj)
  271.             {
  272.                 Parametrs_for_thread param = (Parametrs_for_thread)x;
  273.                 List<int> current_simple_numbers = Program.first_simple_numbers_list[param.elements_count_num];
  274.                 foreach(var simple_number in current_simple_numbers)
  275.                 {
  276.                     bool br = false;
  277.                     foreach(var cheked in cheked_simple_m4)
  278.                     {
  279.                         if(cheked == simple_number)
  280.                         {
  281.                             br = true;
  282.                         }
  283.                     }
  284.                     if (br) { continue; }
  285.                     cheked_simple_m4.Add(simple_number);
  286.                     for(int i = 0; i < second_simple_numbers_m4.Count; i++)
  287.                     {
  288.                         if(i%simple_number == 0)
  289.                         {
  290.                             remove_numbers_m4.Add(i);
  291.                         }
  292.                     }
  293.                 }
  294.             }
  295.         }
  296.     }
  297.     public class Parametrs_for_thread
  298.     {
  299.         public int thread_num;
  300.         public int thread_count;
  301.         public int elements_count_num;
  302.     }
  303. }
Add Comment
Please, Sign In to add comment