# Is using an If[] condition faster than KronckerDelta[]?

I have a code which has a term in a summation like:

KroneckerDelta[M,0]*func[M]


where func[M] is a time consuming numerical integral with a parameter M. I am looping over different values of M.

My question is, does Mathematica evaluate func[M] even if the KroneckerDelta[M,0] evaluates to 0 (i.e. when M != 0)? Or does it recognize that since the KroneckerDelta[M,0] is giving 0, so the product must be zero and hence doesn't actually evaluate the func[M] part then?

Because if it is evaluating the time consuming numerical integral in func[M] anyway, I feel using an If[M=0] like statement would save computation time...? Right?

Thanks.

• This Table[KroneckerDelta[1, M] Pause[M], {M, 1, 3}] // AbsoluteTiming takes six seconds. Commented Mar 13, 2020 at 1:24
• To clarify: Times in Mathematica does not short-circuit multiplication by zero. Indeed, it cannot since Times has no HoldAll attribute. This means that all it's arguments must be evaluated before Times does anything at all. Compare this with And and Or, which DO have the the HoldAll attribute and will short-circuit the moment they encounter False / True respectively. Commented Mar 13, 2020 at 11:42
• @SjoerdSmit Gotcha. Thanks. Makes sense. Thanks, everyone! Commented Mar 14, 2020 at 22:37

func[x_] := Pause[1];