Facebook
From Funky Porcupine, 7 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 290
  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. namespace zd2_3
  11. {
  12.     class zd2_2
  13.     {
  14.         static public int[] tab;
  15.  
  16.         static public void nowa_tablica(int rozmiar)
  17.         {
  18.             int[] tab = new int[rozmiar];
  19.  
  20.             Random rand = new Random();
  21.             Stopwatch czas = new Stopwatch();
  22.  
  23.  
  24.             for (int i = 0; i < rozmiar;i++)
  25.             {
  26.                 tab[i] = rand.Next(0, 100);
  27.             }
  28.  
  29.  
  30.             suma(tab);
  31.             suma_bez_watkow(tab,rozmiar);
  32.         }
  33.  
  34.  
  35.    
  36.         static public void suma(int[] tab)
  37.         {
  38.             int suma = 0;
  39.          
  40.             Parallel.For(0, tab.Length, () => 0, (j, loop, subtotal) =>
  41.                 {
  42.                     subtotal += tab[j];
  43.                     return subtotal;
  44.                 },
  45.                         (x) => Interlocked.Add(ref suma, x));
  46.  
  47.          
  48.             Console.WriteLine("Suma: " + suma);
  49.        
  50.         }
  51.  
  52.  
  53.         static void suma_bez_watkow(int[] tab, int rozmiar)
  54.         {
  55.             int suma=0;
  56.             for (int i = 0; i <rozmiar;i++ )
  57.             {
  58.                 suma = suma + tab[i];
  59.             }
  60.             Console.WriteLine("Suma bez watkow: " + suma);
  61.         }
  62.  
  63.  
  64.  
  65.  
  66.         static void Main(string[] args)
  67.         {
  68.             int rozmiar;
  69.             Console.WriteLine("Podaj wielkość tablicy: ");
  70.             rozmiar = Convert.ToInt32(Console.ReadLine());
  71.  
  72.             nowa_tablica(rozmiar);
  73.  
  74.             Console.ReadLine();
  75.  
  76.         }
  77.     }
  78. }