-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
100 lines (87 loc) · 1.97 KB
/
Source.cpp
File metadata and controls
100 lines (87 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//ïðîòåñòîâàíî íà 2,5,10,20 ïîòîêàõ äî 10 âèäíî âèãðàø íà 20 âèäíî ïðîãðàø
#include <iostream>
#include <thread>
#include <ctime>
using namespace std;
void summing(int** m1, int** m2, int** res, int from, int to, int n)
{
for (int i = from;i < to;++i)
{
for (int j = 0;j < n;++j)
{
res[i][j] = m1[i][j] + m2[i][j];
}
}
}
void subtraction(int** m1, int** m2, int** res, int from, int to, int n)
{
for (int i = from;i < to;++i)
{
for (int j = 0;j < n;++j)
{
res[i][j] = m1[i][j] - m2[i][j];
}
}
}
void main()
{
srand(time(NULL));
int n = 5000;
srand(time(NULL));
int** arr1 = new int*[n];
int** arr2 = new int*[n];
for (int i = 0;i < n;++i)
{
arr1[i] = new int[n];
arr2[i] = new int[n];
}
int** res1 = new int*[n];
int** res2 = new int*[n];
for (int i = 0;i < n;++i)
{
res1[i] = new int[n];
res2[i] = new int[n];
}
for (int i = 0;i < n;++i)
{
for (int j = 0;j < n;++j)
{
arr1[i][j] = rand() % 100;
arr2[i][j] = rand() % 100;
}
}
int numberOfThreads = 5;
thread* arr = new thread[numberOfThreads];
int from = 0;
int step = n/numberOfThreads;
int to = step;
clock_t start_time = clock();
for (int i = 0;i < numberOfThreads;++i)
{
arr[i] = thread(summing,arr1, arr2, res1, from, to, n);
arr[i].join();
from += step ;
to += step ;
}
cout <<"summing time for n=5000 and numberof threads="<<numberOfThreads<<" "<< (float)clock() - start_time << endl;
/*for (int i = 0;i < n;++i)
{
for (int j = 0;j < n;++j)
{
cout << res1[i][j] << " ";
}
cout << endl;
}*/
from = 0;
to = step;
start_time = clock();
for (int i = 0;i < numberOfThreads;++i)
{
arr[i] = thread(subtraction, arr1, arr2, res1, from, to, n);
arr[i].join();
from += step;
to += step;
}
cout << "subtraction time for n=5000 and numberof threads=" << numberOfThreads << " " << (float)clock() - start_time << endl;
system("pause");
}