Skip to content

Instantly share code, notes, and snippets.

@phg1024
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save phg1024/cc744696bbbb93a673c5 to your computer and use it in GitHub Desktop.

Select an option

Save phg1024/cc744696bbbb93a673c5 to your computer and use it in GitHub Desktop.
effect of cache demonstration
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
const int MAX = 10000;
vector<vector<double>> A(MAX, vector<double>(MAX));
vector<double> x(MAX), y(MAX);
for(int i=0;i<MAX;++i)
for(int j=0;j<MAX;++j) A[i][j] = rand();
for(int i=0;i<MAX;++i) x[i] = rand();
clock_t ts, te;
ts = clock();
for(int i=0;i<MAX;++i) {
for(int j=0;j<MAX;++j) {
y[i] += A[i][j] * x[j];
}
}
te = clock();
cout << te - ts << endl;
ts = clock();
for(int j=0;j<MAX;++j) {
for(int i=0;i<MAX;++i) {
y[i] += A[i][j] * x[j];
}
}
te = clock();
cout << te - ts << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment