GreenMap

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

Feb 17th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Diagnostics;
  8.  
  9.  
  10. class Program
  11. {
  12.     static int[] N = {10, 100, 1000, 100000};
  13.     static int[] M = {2, 3, 4, 5, 10};
  14.     const double c = 114.105;
  15.     const int posled_start = 10;
  16.     const int potok_start = 100;
  17.     static void Main() {
  18.         foreach(var n in N){
  19.             Stopwatch time = new Stopwatch();
  20.             time.Start();
  21.             for(int j = 0; j<posled_start;j++){
  22.                 double[] mass_posl = new double[n];
  23.                 for(int i = 0; i<n; i++){
  24.                     mass_posl[i] = i*c;
  25.                 }
  26.             }
  27.             time.Stop();
  28.             Console.WriteLine("Total time последовательной обработки при N = {0}: {1} ms.", n, time.Elapsed.TotalMilliseconds/posled_start);
  29.         }
  30.         foreach(var n in N){
  31.             foreach(var m in M){
  32.                 Stopwatch time = new Stopwatch();
  33.                 time.Start();
  34.                 for(int j = 0; j<potok_start;j++){
  35.                     Thread[] threads = new Thread[m];
  36.                     double[] mass = new double[n];
  37.                     for (int i = 0; i < m; i++)
  38.                     {
  39.                         Param param = new Param(n, m, i, mass);
  40.                         threads[i] = new Thread(param.Work);
  41.                         threads[i].Start();
  42.                     }
  43.                     for (int i = 0; i < m; i++)
  44.                         threads[i].Join();
  45.                 }
  46.                 time.Stop();
  47.                 Console.WriteLine("Total time паралельной обработки при N = {0} и M = {1}: {2} ms.", n, m, time.Elapsed.TotalMilliseconds/potok_start);
  48.             }
  49.         }
  50.     }
  51.    
  52. }
  53. public class Param{
  54.     const double c = 5.7;
  55.     private int n;
  56.     private int m;
  57.     private int o;
  58.     private double[] mass;
  59.     public Param(int _n, int _m, int _o, double[] _mass){
  60.         this.n = _n;
  61.         this.m = _m;
  62.         this.o = _o;
  63.         this.mass = _mass;
  64.     }
  65.     public void Work()
  66.         {
  67.             int ThreadNumber = (int)o;
  68.             int part = n / m;
  69.             for (int i = ThreadNumber * part; i < (ThreadNumber + 1) * part; i++)
  70.             {
  71.                 mass[i] = i*c;
  72.             }
  73.         }
  74. }
Add Comment
Please, Sign In to add comment