Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ThreadingTPM
- {
- class Program
- {
- const int size = 10000;
- const int thrCount = 10;
- const int space = size / thrCount;
- static void Main(string[] args)
- {
- float[,] arr1 = new float[size, size];
- float[,] arr2 = new float[size, size];
- Random r = new Random();
- Thread[] threads = new Thread[thrCount];
- Stopwatch watch = new Stopwatch();
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- arr1[i, j] = (float)r.NextDouble() * r.Next(-1000, 1000);
- arr2[i, j] = (float)r.NextDouble() * r.Next(-1000, 1000);
- }
- }
- int count = 0;
- watch.Start();
- for (int j = 0; j < thrCount; j++)
- {
- threads[j] = new Thread(delegate () { DiffFunc(arr1, arr2, count); });
- threads[j].Start();
- threads[j].Join();
- count++;
- }
- watch.Stop();
- Console.WriteLine(string.Format("Threads ({0}): {1}", thrCount, watch.Elapsed));
- }
- static void DiffFunc(float[,] arr1, float[,] arr2, int count)
- {
- int x = count * space;
- for (int i = x; i < x + space; i++)
- {
- for (int j = 0; j < size; j++)
- {
- arr1[i, j] += arr2[i, j];
- arr1[i, j] = arr1[i, j] * arr1[i, j];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement