-
Notifications
You must be signed in to change notification settings - Fork 41
/
matrix_mul(2).cpp
62 lines (58 loc) · 1.69 KB
/
matrix_mul(2).cpp
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
Assignment No:01
ALI MD YOUSUF
LB2021036
.....................
#include<iostream>
#include<cstring>
#include<Windows.h>
constexpr auto MAT_SIZE = 5000;;
using namespace std;
int main() {
int (*mat)[MAT_SIZE] = new int[MAT_SIZE][MAT_SIZE];
int (*result)[MAT_SIZE] = new int[MAT_SIZE][MAT_SIZE];
memset(mat, -1, MAT_SIZE * MAT_SIZE * sizeof(int));
memset(result, 0, MAT_SIZE * MAT_SIZE * sizeof(int));
LARGE_INTEGER t1, t2, tc;
QueryPerformanceFrequency(&tc);
// funtion 1
QueryPerformanceCounter(&t1);
for (size_t i = 0; i < MAT_SIZE; i++)
{
for (size_t j = 0; j < MAT_SIZE; j++) {
for (size_t k = 0; k < MAT_SIZE; k++)
{
result[i][j] += mat[i][k] * mat[k][j];
}
}
}
QueryPerformanceCounter(&t2);
cout << "time1 = " << (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart << endl; // 71.6786
// function 2
memset(result, 0, MAT_SIZE * MAT_SIZE * sizeof(int));
QueryPerformanceCounter(&t1);
for (size_t j = 0; j < MAT_SIZE; j++)
{
for (size_t i = 0; i < MAT_SIZE; i++) {
for (size_t k = 0; k < MAT_SIZE; k++)
{
result[i][j] += mat[i][k] * mat[k][j];
}
}
}
QueryPerformanceCounter(&t2);
cout << "time2 = " << (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart << endl; // 21.2423
// function 3
memset(result, 0, MAT_SIZE * MAT_SIZE * sizeof(int));
QueryPerformanceCounter(&t1);
for (size_t k = 0; k < MAT_SIZE; k++)
{
for (size_t i = 0; i < MAT_SIZE; i++) {
for (size_t j = 0; j < MAT_SIZE; j++)
{
result[i][j] += mat[i][k] * mat[k][j];
}
}
}
QueryPerformanceCounter(&t2);
cout << "time3 = " << (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart << endl; // 16.9107
}