We all know about short circuiting in logical expressions, i.e. when
if ( False AND myFunc(a) ) then
...
doesn't bother executing myFunc()
because there's no way the if
condition can be true.
I was curious as to whether there is an equivalent for your everyday algebraic equation, say
result = C*x/y + z
If C=0
there is no point in evaluating the first term. It wouldn't matter much performance-wise if x
and y
were scalars, but if we pretend they are large matrices and the operations are costly (and applicable to matrices) then surely it would make a difference. Of course you could avoid such an extreme case by throwing in an if C!=0
statement.
So my question is whether such a feature exists and if it is useful. I'm not much of a programmer so it probably does under some name that I haven't come across; if so please enlighten me :)
result = C*myfunction()
. IfC==0
, causing the arithmetic expression to short-circuit, thenmyfunction
is never invoked, and whatever side-effects it might have had do not occur (just as with logical short-circuiting). – Knoll