Reputation: 275
Which logic would be better or effecient:
IF VAR-A = FALSE
MOVE VAR-C TO VAR-B
ELSE
MOVE VAR-A TO VAR-B
END-IF
or
MOVE VAR-A TO VAR-B
IF VAR-A = FALSE
MOVE VAR-C TO VAR-B
END-IF
Upvotes: 1
Views: 1746
Reputation: 2109
The first is more efficient, unless VAR-A is never false (in which case just drop it). The condition must always run, and one assignment must always occur, so this is optimal:
IF VAR-A = FALSE
MOVE VAR-C TO VAR-B
ELSE
MOVE VAR-A TO VAR-B
END-IF
Personally, I prefer evaluating for TRUE conditions first, for ease of reading.
IF VAR-A = TRUE
MOVE VAR-A TO VAR-B
ELSE
MOVE VAR-C TO VAR-B
END-IF
There is no performance difference, because either way it's one branch. If you were checking for multiple values, you always want to put the most-common first.
EVALUATE TRUE
WHEN VAR-A = TRUE
MOVE VAR-A TO VAR-B
WHEN VAR-C = TRUE
MOVE VAR-C TO VAR-B
WHEN OTHER
this is slower, because it had to do two condition checks to get here
MOVE VAR-C TO VAR-B
END-EVALUTE
But that does not apply to the simple IF, as you can see:
IF condition
no effort to get here
assignment cost is constant
one go-to to get to the next line
ELSE
one go-to to get here
assignment cost is constant
no effort to get to the next line
END-IF
But this is really all nit-picking. We're talking about a couple instructions here. Unless this is running in an inner loop that runs 1,000,000 times a minute, don't even think about it.
Upvotes: 1
Reputation: 4263
In general, either is fine and you should leave the optimization to your compiler.
However, since source code is written for humans to understand what prior programmers have created, I would say:
Use this form if either condition is likely to occur:
IF VAR-A = FALSE
MOVE VAR-C TO VAR-B
ELSE
MOVE VAR-A TO VAR-B
END-IF
But in a case where the IF condition is a very rare kind of thing and the unqualified MOVE is the 99% condition, try this approach, as it highlights the fact that VAR-A=FALSE is a rare and special thing:
MOVE VAR-A TO VAR-B
IF VAR-A = FALSE
MOVE VAR-C TO VAR-B
END-IF
Just my NSH $0.02. Either works well as long as you remember that the compiler will take most any crap you feed it, your primary concern is the next human that must read your program.
Upvotes: 2
Reputation: 782
But I prefer this way of coding,
MOVE VAR-A TO VAR-B
IF VAR-A = FALSE
MOVE VAR-C TO VAR-B
END-IF
Its because, in order to process/execute the conditions/loops/evaluate statements compiler needs an attention on them and writes the equivalent compiler code for this.
For instance, if we have scenarios where FALSE scenario occurs for less than 50% then we can move the value into the variable for TRUE always, which may avoid going into the IF and IF with no ELSE is always less complex.
What do you say?
Upvotes: 1
Reputation: 19443
I would prefer this:
IF VAR-A = TRUE
MOVE VAR-A TO VAR-B
ELSE
MOVE VAR-C TO VAR-B
END-IF
In general, it's hard to read taking actions on an if false
, so I prefer to state things positively. And I don't think the efficiency difference really matters (particularly if you are using COBOL). The compiler will usually do the right thing anyways.
Upvotes: 3