#include #include #include #include #include #include int main( int argc, char** argv ) { int x_size = 256; int y_size = 256; if ( argc > 1 ) { x_size = atoi ( argv [ 1 ] ); } if ( argc > 2 ) { y_size = atoi ( argv [ 2 ] ); } int size = x_size * y_size * sizeof ( int ); int* array = malloc ( size ); printf ( "Just allocated an array of size %d\n", size ); int i = 0; for ( int x = 0; x < x_size; x++ ) { for ( int y = 0; y < y_size; y++ ) { i = x * y_size + y; array [ i ] += 1; } } printf ( "\nFinished, i = %d\n", i ); return 0; } // The two for loops can be exchanged without affecting the result of // the program, only its run-time (assuming x_size and y_size are // large enough). I conjecture (but am not sure) that this is because // of cache effects: one ordering of the for loops is cache friendly // (= exhibits a lot of locality), while the other destroys most // locality (because the array access pattern is too erratic).