I am writing an algorithm to find the inverse of an nxn matrix. Let us take the specific case of a 3x3 matrix.
When you invert a matrix by hand, you typically look for rows/columns containing one or more zeros to make the determinant calculation faster as it eliminates terms you need to calculate.
Following this logic in C/C++, if you identify a row/column with one or more zeros, you will end up with the following code:
float term1 = currentElement * DetOf2x2(...);
// ^
// This is equal to 0.
//
// float term2 = ... and so on.
As the compiler cannot know currentElement
will be zero at compile time, it cannot be optimised to something like float term = 0;
and thus the floating point multiplication will be carried out at runtime.
My question is, will these zero values make the floating point multiplication faster, or will the multiplication take the same amount of time regardless of the value of currentElement
? If there is no way of optimising the multiplication at runtime, then I can remove the logic that searches for rows/columns containing zeros.
float term1 = currentElement * DetOf2x2(...);
The compiler will call DetOf2x2(...)
even if currentElement is 0: that's sure to be far more costly than the final multiplication, whether by 0 or not. There are multiple reasons for that:
DetOf2x2(...)
may have side effects (like output to a log file) that need to happen even when currentElement
is 0
, andDetOf2x2(...)
may return values like the Not-a-Number / NaN sentinel that should propagate to term1
anyway (as noted first by Nils Pipenbrinck)Given DetOf2x2(...)
is almost certainly working on values that can only be determined at run-time, the latter possibility can't be ruled out at compile time.
If you want to avoid the call to Detof2x2(...)
, try:
float term1 = (currentElement != 0) ? currentElement * DetOf2x2(...) : 0;