在以下代码中,为什么f2()
比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);
}
}
}
优化是禁用,x64版本
我知道缓存,但为什么它不能在这里工作...
//当n大于N时,f1()
比f2()
更快
//我有i5 HQ4750 2ghz,8mb缓存