In the following code, why is f2()
is faster then f1()
?
const int n = 3;
const int m = 4096;
void f1()
{
int mas[n][m];
//int mas_1[n][m];
for (int i = 0; i < n; ++i)
{
for (int i_1 = 0; i_1 < m; ++i_1)
{
mas[i][i_1] = 1;
//mas_1[i][i_1] = 1;
}
}
}
void f2()
{
int mas[n][m];
//int mas_1[n][m];
for (int i = 0; i < m; ++i)
{
for (int i_1 = 0; i_1 < n; ++i_1)
{
mas[i_1][i] = 1;
//mas_1[i_1][i] = 1;
}
}
}
int main()
{
for (size_t a = 0; a < 10; ++a)
{
{
clock_t s = clock();
for (size_t i = 0; i < 10000; ++i)
{
f1();
}
clock_t e = clock();
printf("T1: %d\n", e - s);
}
{
clock_t s = clock();
for (size_t i = 0; i < 10000; ++i)
{
f2();
}
clock_t e = clock();
printf("T2: %d\n", e - s);
}
}
}
Optimisation is disable, x64 release
I know about caching, but why it doesn't work here ...
// When n is greater then some N, f1()
is faster then f2()
// I have i5 HQ4750 2ghz, 8mb cache