Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Lab_2_AAC
- {
- class Program
- {
- public static int[] n = { 1000, 10000, 100000 }; //колличество элементов должно быть = колличеству эл. в thread_count_for_1
- public static List<List<int>> first_simple_numbers_list = new List<List<int>>();
- public static List<int> second_simple_numbers_m1 = new List<int>();
- public static List<int> second_simple_numbers_m2 = new List<int>();
- public static List<int> second_simple_numbers_m3 = new List<int>();
- public static List<int> second_simple_numbers_m4 = new List<int>();
- public static int _number_not_finished_threads_m3;
- public static ManualResetEvent finish_work_event = new ManualResetEvent(false);
- public static List<int> remove_numbers_m2 = new List<int>();
- public static List<int> remove_numbers_m3 = new List<int>();
- public static List<int> remove_numbers_m4 = new List<int>();
- public static List<int> cheked_simple_m4 = new List<int>();
- public static double[] avarage_performance_time = new double[4];
- public static object lockObj = new object();
- static int[] thread_count_for_1 = { 1, 5, 10 };
- static void Main(string[] args)
- {
- for (int i = 0; i < n.Length; i++)
- {
- first_simple_numbers_list.Add(new List<int>());
- first_simple_numbers_list[i].Add(2);
- }
- for (int list_num = 0; list_num < first_simple_numbers_list.Count; list_num++)
- {
- for (int i = 2; i < Convert.ToInt32(Math.Sqrt(n[list_num])); i++)
- {
- bool simple = true;
- foreach (var j in first_simple_numbers_list[list_num])
- {
- if (i % j == 0)
- {
- simple = false;
- }
- }
- if (simple)
- {
- first_simple_numbers_list[list_num].Add(i);
- }
- }
- }
- if (first_simple_numbers_list[0].Count < thread_count_for_1[thread_count_for_1.Length - 1])
- {
- Console.WriteLine("Потоков больше чем простых чисел!");
- }//проверка на ошибки
- List_Printer(first_simple_numbers_list[0]);
- //method 1
- int all_time = 0;
- for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
- {
- foreach (var thread_count in thread_count_for_1)
- {
- second_simple_numbers_m1.Clear();
- Stopwatch time = new Stopwatch();
- time.Start();
- Thread[] threads = new Thread[thread_count];
- for (int i = 0; i < thread_count; i++)
- {
- Parametrs_for_thread param = new Parametrs_for_thread();
- param.thread_num = i;
- param.thread_count = thread_count;
- param.elements_count_num = elements_count_num;
- threads[i] = new Thread(Work_m1);
- threads[i].Start(param);
- }
- for (int i = 0; i < thread_count; i++)
- {
- threads[i].Join();
- }
- time.Stop();
- Console.WriteLine("Время обработки с помощью первого метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
- all_time += (int)time.ElapsedMilliseconds;
- }
- //second_simple_numbers_m1 - ответ
- }
- avarage_performance_time[0] = all_time / (n.Length * thread_count_for_1.Length);
- // method 2
- all_time = 0;
- for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
- {
- foreach (var thread_count in thread_count_for_1)
- {
- second_simple_numbers_m2.Clear();
- Stopwatch time = new Stopwatch();
- time.Start();
- for(int i = Convert.ToInt32(Math.Sqrt(n[elements_count_num])); i < n[elements_count_num]; i++)
- {
- second_simple_numbers_m2.Add(i);
- }//заполняем список значениями от корня из н до н чтобы потом удалять ненужные
- Thread[] threads = new Thread[thread_count];
- for (int i = 0; i < thread_count; i++)
- {
- Parametrs_for_thread param = new Parametrs_for_thread();
- param.thread_num = i;
- param.thread_count = thread_count;
- param.elements_count_num = elements_count_num;
- threads[i] = new Thread(Work_m2);
- threads[i].Start(param);
- }
- for (int i = 0; i < thread_count; i++)
- {
- threads[i].Join();
- }
- foreach (var number in remove_numbers_m2)
- {
- second_simple_numbers_m2.Remove(number);
- }
- time.Stop();
- Console.WriteLine("Время обработки с помощью второго метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
- all_time += (int)time.ElapsedMilliseconds;
- //second_simple_numbers_m2 - ответ
- }
- }
- avarage_performance_time[1] = all_time / (double)(n.Length * thread_count_for_1.Length);
- // method 3
- all_time = 0;
- for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
- {
- foreach (var thread_count in thread_count_for_1)
- {
- second_simple_numbers_m3.Clear();
- Stopwatch time = new Stopwatch();
- time.Start();
- for (int i = Convert.ToInt32(Math.Sqrt(n[elements_count_num])); i < n[elements_count_num]; i++)
- {
- second_simple_numbers_m3.Add(i);
- }//заполняем список значениями от корня из н до н чтобы потом удалять ненужные
- _number_not_finished_threads_m3 = first_simple_numbers_list[elements_count_num].Count;
- ThreadPool.SetMaxThreads(thread_count, thread_count);
- for(int simple_number = 0; simple_number < first_simple_numbers_list[elements_count_num].Count; simple_number++)
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(Work_m3), new object[] { first_simple_numbers_list[elements_count_num][simple_number]});
- }
- finish_work_event.WaitOne();
- foreach(var number in remove_numbers_m3)
- {
- second_simple_numbers_m3.Remove(number);
- }
- finish_work_event = new ManualResetEvent(false);
- time.Stop();
- Console.WriteLine("Время обработки с помощью третьего метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
- all_time += (int)time.ElapsedMilliseconds;
- }
- }
- avarage_performance_time[2] = all_time / (n.Length * thread_count_for_1.Length);
- // method 4
- all_time = 0;
- for (int elements_count_num = 0; elements_count_num < n.Length; elements_count_num++)
- {
- foreach (var thread_count in thread_count_for_1)
- {
- second_simple_numbers_m4.Clear();
- Stopwatch time = new Stopwatch();
- time.Start();
- for (int i = Convert.ToInt32(Math.Sqrt(n[elements_count_num])); i < n[elements_count_num]; i++)
- {
- second_simple_numbers_m4.Add(i);
- }//заполняем список значениями от корня из н до н чтобы потом удалять ненужные
- Thread[] threads = new Thread[thread_count];
- for (int i = 0; i < thread_count; i++)
- {
- Parametrs_for_thread param = new Parametrs_for_thread();
- param.thread_num = i;
- param.thread_count = thread_count;
- param.elements_count_num = elements_count_num;
- threads[i] = new Thread(Work_m4);
- threads[i].Start(param);
- }
- for (int i = 0; i < thread_count; i++)
- {
- threads[i].Join();
- }
- foreach (var number in remove_numbers_m4)
- {
- second_simple_numbers_m4.Remove(number);
- }
- time.Stop();
- Console.WriteLine("Время обработки с помощью четвертого метода при потоках = {0}, элементов = {1}, ms: {2}", thread_count, n[elements_count_num], time.ElapsedMilliseconds);
- all_time += (int)time.ElapsedMilliseconds;
- //second_simple_numbers_m4 - ответ
- }
- }
- avarage_performance_time[3] = all_time / (n.Length * thread_count_for_1.Length);
- for(int i = 0; i < avarage_performance_time.Length; i++)
- {
- Console.WriteLine("Среднее время выполнения по {0} методу: {1}", i+1, avarage_performance_time[i]);
- }
- Console.ReadLine();
- }
- static void List_Printer(List<int> list)
- {
- list.Sort();
- foreach (var num in list)
- {
- Console.Write(" {0}", num);
- }
- Console.WriteLine("");
- }
- static void Work_m1(object x)
- {
- Parametrs_for_thread param = (Parametrs_for_thread)x;
- int n_sqrt = Convert.ToInt32(Math.Sqrt(n[param.elements_count_num]));
- int elements_count = (n[param.elements_count_num] - n_sqrt) / param.thread_count;
- for (int i = n_sqrt + elements_count * param.thread_num; i < n_sqrt + elements_count * (param.thread_num + 1); i++)
- {
- bool simple_flag = true;
- foreach (var simple in first_simple_numbers_list[param.elements_count_num])
- {
- if (i % simple == 0)
- {
- simple_flag = false;
- }
- }
- if (simple_flag)
- {
- Program.second_simple_numbers_m1.Add(i);
- }
- }
- }
- static void Work_m2(object x)
- {
- Parametrs_for_thread param = (Parametrs_for_thread)x;
- List<int> current_simple_numbers = Program.first_simple_numbers_list[param.elements_count_num];
- int simple_numbers_begin_num = param.thread_num * (current_simple_numbers.Count / param.thread_count);
- int simple_numbers_end_num = (param.thread_num + 1) * (current_simple_numbers.Count / param.thread_count);
- for (int i = 0; i < Program.second_simple_numbers_m2.Count; i++)
- {
- for (int simple_num = simple_numbers_begin_num; simple_num < simple_numbers_end_num; simple_num++)
- {
- if (Program.second_simple_numbers_m2[i] % current_simple_numbers[simple_num] == 0)
- {
- remove_numbers_m2.Add(second_simple_numbers_m2[i]);
- break;
- }
- }
- }
- }
- static void Work_m3(object x)
- {
- int simple_number = (int)((object[])x)[0];
- for (int number = 0; number < second_simple_numbers_m3.Count; number++)
- {
- if(second_simple_numbers_m3[number] % simple_number == 0)
- {
- remove_numbers_m3.Add(second_simple_numbers_m3[number]);
- }
- }
- if(Interlocked.Decrement(ref _number_not_finished_threads_m3) == 0)
- {
- finish_work_event.Set();
- }
- }
- static void Work_m4(object x)
- {
- lock (lockObj)
- {
- Parametrs_for_thread param = (Parametrs_for_thread)x;
- List<int> current_simple_numbers = Program.first_simple_numbers_list[param.elements_count_num];
- foreach(var simple_number in current_simple_numbers)
- {
- bool br = false;
- foreach(var cheked in cheked_simple_m4)
- {
- if(cheked == simple_number)
- {
- br = true;
- }
- }
- if (br) { continue; }
- cheked_simple_m4.Add(simple_number);
- for(int i = 0; i < second_simple_numbers_m4.Count; i++)
- {
- if(i%simple_number == 0)
- {
- remove_numbers_m4.Add(i);
- }
- }
- }
- }
- }
- }
- public class Parametrs_for_thread
- {
- public int thread_num;
- public int thread_count;
- public int elements_count_num;
- }
- }
Add Comment
Please, Sign In to add comment