During my internship, I have to use nested for loops, and I wrote something like this (in C) :
for (int N_dt0_s_dt = 0; N_dt0_s_dt < N_dt; N_dt0_s_dt++)
{
for (int j = 0; j < N; j++)
{
for (int i = 0; i < N0; i++)
{
Matrix_t0[i + N0 * j] += M_t_t0_loc[i + N0 *(N_dt0_s_dt + j)];
}
}
}
Here, N and N0 are integers representing the matrix dimensions. N and N0 are between 200 and 500, depending on the input matrix. During the review of the code, some coworker suggested the following changes :
int dec,dec1;//to compute index
for (int N_dt0_s_dt = 0; N_dt0_s_dt < N_dt; N_dt0_s_dt++)
{
for (int j = 0; j < N; j++)
{
dec = N0 * j;
dec1 = N0 *(N_dt0_s_dt + j);
for (int i = 0; i < N0; i++)
{
Matrix_t0[i + dec] += M_t_t0_loc[i + dec1];
}
}
}
}
The coworker told me the latter should run faster, because we are not computing the operations needed to come up with the N0 *(N_dt0_s_dt + j) at each iteration.
I think it is pretty legitimate, but I was asking myself if the compiler (Visual Studio) was already doing optimizations which would make those changes useless.
Some thoughts on this would be appreciated.