We read about different instances in C++ where temporary objects are created in code. See for example. Now, consider the following snippet of code.
const int rows {250};
const int cols {250};
const double k {0.75};
double A[rows][cols];
double B[rows][cols];
double C[rows][cols];
// Code that initialises A and B
...
for (int i{}; i < rows; ++i) {
for (int j{}; j < cols; ++j) {
C[i][j] = k * A[i][j] * B[i][j] / (A[i][j] * A[i][j] + B[i][j] * B[i][j]);
}
}
Are temporaries created when evaluating the numerator and denominator in the RHS of the equation in the inner for loop? Obviously, one can evaluate the expression in parts and store the results in intermediate variables as below.
for (int i{}; i < rows; ++i) {
for (int j{}; j < cols; ++j) {
double temp1 = A[i][j] * B[i][j];
double temp2 = k * temp1;
double temp3 = A[i][j] * A[i][j];
double temp4 = B[i][j] * B[i][j];
double temp5 = temp3 + temp4;
C[i][j] = temp2 / temp5;
}
}
Doesn't the latter approach introduce additional computational steps and therefore more overhead for the inner for loop?