Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. void rowMajor(vector<vector<int>> &v) {
  8.     cout << "Benchmarking row major: " << endl;
  9.     time_t start = clock();
  10.     unsigned int sum = 0;
  11.     for (int i = 0; i < v.size(); i++)
  12.         for (int j = 0; j < v[0].size(); j++)
  13.             sum += v[i][j];
  14.     time_t end = clock();
  15.     double timeTaken = (double(end - start))/CLOCKS_PER_SEC;
  16.     cout << "sum is " << sum << endl;
  17.     cout << "Time taken: " << timeTaken << endl;
  18. }
  19.  
  20. void colMajor(vector<vector<int>> &v) {
  21.     cout << "Benchmarking column major: " << endl;
  22.     time_t start = clock();
  23.     unsigned int sum = 0;
  24.     for (int i = 0; i < v[0].size(); i++)
  25.         for (int j = 0; j < v.size(); j++)
  26.             sum += v[j][i];
  27.     time_t end = clock();
  28.     double timeTaken = (double(end - start))/CLOCKS_PER_SEC;
  29.     cout << "sum is " << sum << endl;
  30.     cout << "Time taken: " << timeTaken << endl;
  31. }
  32.  
  33. int main() {
  34.     const int COL_SIZE = 20000;
  35.     const int ROW_SIZE = 40000;
  36.     const int UPPER_BOUND = 50;
  37.  
  38.     // 1d array
  39.     vector<int> v1(COL_SIZE, rand() % UPPER_BOUND);
  40.  
  41.     // 2d array
  42.     vector<vector<int>> v2(ROW_SIZE, v1);
  43.  
  44.     rowMajor(v2);
  45.  
  46.     colMajor(v2);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement