[code]

#include <chrono>
#include <iostream>

using namespace std;

typedef chrono::high_resolution_clock::time_point Clock;
typedef chrono::high_resolution_clock::duration Diff;
typedef chrono::high_resolution_clock Timer;

static const int SIZE = 16384;
volatile int array[SIZE][SIZE];

void loop1() {
  for (int i = 0; i < SIZE; i++)
    for (int j = 0; j < SIZE; j++)
      array[i][j] = 0;
}

void loop2() {
  for (int i = 0; i < SIZE; i++)
    for (int j = 0; j < SIZE; j++)
      array[j][i] = 0;
}

int main() {
  Clock c1 = Timer::now();
  loop1();
  Clock c2 = Timer::now();
  loop2();
  Clock c3 = Timer::now();

  Diff elapsed1 = Diff(c2 - c1);
  clog << "loop1 elapsed " << elapsed1.count() << endl;

  Diff elapsed2 = Diff(c3 - c2);
  clog << "loop2 elapsed " << elapsed2.count() << endl;

  return 0;
}




[build]


g++ -o test test.cpp




[result]


loop1 elapsed 838397292

loop2 elapsed 8154374231