Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <ctime>
- using namespace std;
- void rowMajor(vector<vector<int>> &v) {
- cout << "Benchmarking row major: " << endl;
- time_t start = clock();
- unsigned int sum = 0;
- for (int i = 0; i < v.size(); i++)
- for (int j = 0; j < v[0].size(); j++)
- sum += v[i][j];
- time_t end = clock();
- double timeTaken = (double(end - start))/CLOCKS_PER_SEC;
- cout << "sum is " << sum << endl;
- cout << "Time taken: " << timeTaken << endl;
- }
- void colMajor(vector<vector<int>> &v) {
- cout << "Benchmarking column major: " << endl;
- time_t start = clock();
- unsigned int sum = 0;
- for (int i = 0; i < v[0].size(); i++)
- for (int j = 0; j < v.size(); j++)
- sum += v[j][i];
- time_t end = clock();
- double timeTaken = (double(end - start))/CLOCKS_PER_SEC;
- cout << "sum is " << sum << endl;
- cout << "Time taken: " << timeTaken << endl;
- }
- int main() {
- const int COL_SIZE = 20000;
- const int ROW_SIZE = 40000;
- const int UPPER_BOUND = 50;
- // 1d array
- vector<int> v1(COL_SIZE, rand() % UPPER_BOUND);
- // 2d array
- vector<vector<int>> v2(ROW_SIZE, v1);
- rowMajor(v2);
- colMajor(v2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement