cache-locality

Cache misses when accessing an array in nested loop

落爺英雄遲暮 提交于 2021-02-15 06:50:33
问题 So I have this question from my professor, and I can not figure out why vector2 is faster and has less cache misses than vector1 . Assume that the code below is a valid compilable C code. Vector2: void incrementVector2(INT4* v, int n) { for (int k = 0; k < 100; ++k) { for (int i = 0; i < n; ++i) { v[i] = v[i] + 1; } } } Vector1: void incrementVector1(INT4* v, int n) { for (int i = 0; i < n; ++i) { for (int k = 0; k < 100; ++k) { v[i] = v[i] + 1; } } } NOTE: INT4 means the integer is 4 Bytes