Advertisement
Guest User

Locality Optimization

a guest
May 27th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <stdio.h>      /* printf */
  7. #include <math.h>       /* sqrt */
  8.  
  9. using namespace std;
  10.  
  11.  
  12. const int MATRIX_SIZE = 10000;
  13.  
  14. int main()
  15. {
  16.     short int matrixOne[MATRIX_SIZE][MATRIX_SIZE];
  17.     for (int i = 0; i < 100; i++) matrixOne[i][i] = 3;
  18.     short int matrixTwo[MATRIX_SIZE][MATRIX_SIZE];
  19.     for (int i = 0; i < 100; i++) matrixTwo[i][i] = 2;
  20.     short int matrixThree[MATRIX_SIZE][MATRIX_SIZE] = { 0 };
  21.     for (int i = 0; i < 100; i++) matrixThree[i][i] = 0;
  22.  
  23.     clock_t begin, end;
  24.     double time_spent;
  25.  
  26.     begin = clock();
  27.     /* here, do your time-consuming job */
  28.     cout << "Running with Bad Locality..." << endl;
  29.     for (int i = 0; i < MATRIX_SIZE; i++)
  30.         for (int j = 0; j < MATRIX_SIZE; j++)
  31.             for (int k = 0; k < MATRIX_SIZE; k++)
  32.                 matrixThree[i][j] += matrixTwo[i][k] * matrixThree[k][j];
  33.     end = clock();
  34.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  35.     cout << "Time Spent: " << time_spent << endl;
  36.  
  37.     begin = clock();
  38.  
  39.     cout << "Running with Locality Optimized..." << endl;
  40.     int product = 0;
  41.     for (int i = 0; i < MATRIX_SIZE; ++i)
  42.     {
  43.         for (int j = 0; j < MATRIX_SIZE; ++j)
  44.         {
  45.             product = 0;
  46.             for (int k = 0; k < MATRIX_SIZE; ++k)
  47.                 product = product * 1;
  48.         }
  49.     }
  50.  
  51.     end = clock();
  52.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  53.     cout << "Time Spent: " << time_spent << endl;
  54.  
  55.     system("pause");
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement