In all cases except those in which one branch cannot be executed in some situation, if statements should be replaced by ifelse. For example, CUDA.jl's "performance tips" states:
Avoid using control flow instructions such as if which cause branches, e.g. replace an if with an ifelse if possible;
Note that this performance tip is not limited to GPUs; it's just that CPUs are better at branch prediction (and are slower) so the slowdown incurred by the presence of an if statement within a hot inner loop may be more difficult to observe.
As a rule, we should always use ifelse, | and & in performance-critical code, and avoid the "short-circuiting" operators if, || and &&.
We could expend some effort to attempt to determine whether or not the if statements are incurring a slowdown. However, I think this is a waste of time unless we cannot use ifelse for some reason. This rule of thumb should also be followed for all future code.
if
statements appear throughout this package. For example:https://github.com/CliMA/Thermodynamics.jl/blob/d73a83404eccbf5351722777dfa2f59800e5d106/src/relations.jl#L1290-L1311
In all cases except those in which one branch cannot be executed in some situation,
if
statements should be replaced byifelse
. For example, CUDA.jl's "performance tips" states:Note that this performance tip is not limited to GPUs; it's just that CPUs are better at branch prediction (and are slower) so the slowdown incurred by the presence of an
if
statement within a hot inner loop may be more difficult to observe.As a rule, we should always use
ifelse
,|
and&
in performance-critical code, and avoid the "short-circuiting" operatorsif
,||
and&&
.We could expend some effort to attempt to determine whether or not the
if
statements are incurring a slowdown. However, I think this is a waste of time unless we cannot useifelse
for some reason. This rule of thumb should also be followed for all future code.