How would && operators increase efficiency? I cannot retrieve the question exactly but it is similar to:
We have functions f and g, they always return the same values (say 3 and 4 respectively), and they both take a long time to run. The
Does f()==3 && g()==4 return TRUE faster than f()==3 & g()==4
and it is my understanding from my grade on the assignment that it does.
Double operators could save time if in an "or" statement you would only have to run the first function, compare it to a value, and stop if
it returned a TRUE. In "and" statements you still have to evaluate both functions f and g to see if they both return a particular value. Why
would this change the efficiency at all?
Answers and follow-up questions Answer or follow-up question 1
With the double 'or' operator (||) you get efficiency gains when the first condition is TRUE.
With the double 'and' operator (&&) you get efficiency gains when the first condition is FALSE.
In your example there would be no efficiency gains. If f() would return a number different from 3 there would be efficiency gains because R
would skip the second condition (there is no chance that both conditions are TRUE so the overall result is FALSE.
Michel BallingsSign in to be able to add an answer or mark this question as resolved.