-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
110 lines (88 loc) · 2.35 KB
/
Matrix.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
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
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>
#include <vector>
#include <chrono>
class Matrix
{
public:
Matrix(int n, int m) :
m_n(n),
m_m(m),
m_elements(n * m, 1)
{ }
int operator()(int i, int j) const
{
return m_elements[i * m_m + j];
}
int get_cols() const
{
return m_m;
}
int get_rows() const
{
return m_n;
}
private:
int m_n;
int m_m;
std::vector<int> m_elements;
};
int accumulate_row_wise(const Matrix& mat)
{
auto sum = 0;
auto rows = mat.get_rows();
auto cols = mat.get_cols();
for (auto r = 0; r != rows; r++)
{
for (auto c = 0; c != cols; c++)
{
sum += mat(r, c);
}
}
return sum;
}
int accumulate_column_wise(const Matrix& mat)
{
auto sum = 0;
auto rows = mat.get_rows();
auto cols = mat.get_cols();
for (auto c = 0; c != cols; c++)
{
for (auto r = 0; r != rows; r++)
{
sum += mat(r, c);
}
}
return sum;
}
int main()
{
auto start = 1000;
for (auto i = start; i != 11 * start; i += start)
{
Matrix m(i, i);
auto t1_rows = std::chrono::steady_clock::now();
auto sum_rows = accumulate_row_wise(m);
auto t2_rows = std::chrono::steady_clock::now();
auto t1_cols = std::chrono::steady_clock::now();
auto sum_cols = accumulate_column_wise(m);
auto t2_cols = std::chrono::steady_clock::now();
using time_t = std::chrono::microseconds;
auto time_rows = std::chrono::duration_cast<time_t>(t2_rows - t1_rows).count();
auto time_cols = std::chrono::duration_cast<time_t>(t2_cols - t1_cols).count();
auto factor = 1.0 * time_cols / time_rows;
if (sum_rows == sum_cols)
{
std::cout << "#el = " << std::setw(9) << i * i
<< " time-rows = " << std::setw(6) << time_rows
<< " time-cols = " << std::setw(6) << time_cols
<< " factor = " << std::setw(6) << factor
<< std::endl;
}
else
{
std::cout << "results are wrong" << std::endl;
}
}
return 0;
}