Formula vs. Variable: which is the best way to code mathematic conditions inside IF statement when looking for computation time?

Hi,
I have a very long calculation (several days...) based on many iterations over if-conditons. Most times the conditions are not true so everything else is skipped, so that the if-condition itself seems to be the bottleneck of the computation time. I am wondering where it makes sense for the computation time to do a calculation directly inside the if-conditions and where not:
Example without reuse of Variable:
if sum(Values(Cond))/sum(Durations(Cond)) >= Threshold1
vs.
ConditionedValues = Values(Cond)
if sum(ConditionedValues)/sum(Durations(Cond)) >= Threshold1
Example with reuse:
if sum(Values(Cond))/sum(Durations(Cond)) >= Threshold1 && max(Values(Cond))/sum(Values(Cond)) <= Threshold2
vs.
ConditionedValues = Values(Cond)
if sum(ConditionedValues)/sum(Durations(Cond)) >= Threshold1 && max(ConditionedValues)/sum(ConditionedValues) <= Threshold2
Obviously I suspect that in the first example it should be (slightly?) faster without a variable? If so, by how much are we talking?
And in the second case I suppose it depends how often the first condition is met as only then I jump in the second condition and take profit of the reuse?
Is there a rule of thump or a way to predict the performance without doing the full calculation twice and timing it?
Thanks in Advance!
Best Regards
Seb

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 10 May 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!