Can you tell me which function will work faster? Or perhaps they are both wrong and you know better way to do this. Thanks in advance.
double* solveDiagonal(double* A, double* B, int n)
{
double* X = new double[n];
for(int i = 0; i < n; i++)
X[i] = B[i] / A[i*n + i];
return X;
}
double* solveDiagonal(double* A, double* B, int n)
{
double* X = new double[n];
double** pA = new double*[n];
for(int i = 0; i < n; i++)
pA[i] = &A[i*n];
for(int i = 0; i < n; i++)
X[i] = B[i] / pA[i][i];
delete [] pA;
return X;
}
While I would guess the second one is slower due to more indirection and allocation, you really need to test and profile to answer this kind of question.