In general, does AND replace a couple of nested IFs to speed things up?

I weigh the differences between

If[condition1 AND condition2, A, B]

and

If[condition1, If[condition2, A, B], B]

My thinking: nested IFs will be faster.

My reasoning: by embedding IFs, condition 2 is evaluated only when condition 1 is true. In what I am doing, this means that condition 2 will be evaluated less than in the first option above.

And is the usual logical "and" (true if and only if both assumptions are true). By IF, I mean IF [the condition when the condition is true, when the condition is false].

I hope my question is not too offended; I am sure that there are examples when the first option will work faster. I just hoped that in reality, something was true.

Thank.

+3
4

( ) , ( OR, , - false). .

+5

Mathematica And Or, , .

, , () :

Timing[If[False && (Pause[10]; True), True, False]]
Timing[If[False, If[(Pause[10]; True), True, False], False]]

, , .

+8

Nest[Defer@If[True, #] &, True, 300];

If[True && << 298 >> && True, True];

, , , . If[] , If[] 300 . , True False, , If[] .

+3

?

, , AND OR, , . AND , , , . OR, , , .

if s, . , , .

+1

Source: https://habr.com/ru/post/1777743/


All Articles