Skip to content

Instantly share code, notes, and snippets.

@cwvh
Created May 5, 2014
Embed
What would you like to do?
#include <iostream>
#include <chrono>
#include <cstdlib>
int main() {
const int N = 7*1024;
int** m = new int*[N];
int* n = new int[N*N];
int* k = new int[N*N];
for (int i = 0; i < N; i++) {
m[i] = new int[N];
for (int j = 0; j < N; j++) {
const int r = arc4random();
m[i][j] = r;
n[i*N + j] = r;
k[i*N + j] = r;
}
}
using namespace std::chrono;
time_point<system_clock> start, end;
int elapsed;
start = system_clock::now();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
m[i][j] += 42;
}
}
end = system_clock::now();
elapsed = duration_cast<milliseconds>(end - start).count();
std::cout << elapsed << " ms\n";
start = system_clock::now();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
n[j*N + i] += 42;
}
}
end = system_clock::now();
elapsed = duration_cast<milliseconds>(end - start).count();
std::cout << elapsed << " ms\n";
start = system_clock::now();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
k[i*N + j] += 42;
}
}
end = system_clock::now();
elapsed = duration_cast<milliseconds>(end - start).count();
std::cout << elapsed << " ms\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment