Only check if statement once, or, disable code block after 1 check

In my ODE_func I have several blocks of code have which are toggle on/off based on the settings of the calculations. Unfortunately Matlab checks the if-statements which controls these blocks every time step, resulting in significant overhead.
Is it possible to only check these if-statements once during the entire calculation, thus disabling the code-block for the entire calculation? Or to rephrase the question, can I tell Matlab some variables are constant during the entire calculation so it doesn't have to keep checking the same if-statement.
Many thanks!

Respuestas (3)

You might need to write the same code twice:
ii = 1;
%check
%do your stuff
for ii =2:end
%do your stuff
end

3 comentarios

The problem is that if check==1 then i need to perform certain calculations inside the for-loop for each ii.
ii = 1;
%check
%do your stuff
if check ==1
for ii = 2:end
%do your stuff
end
else
for ii = 2:end
%do your stuff
end
end
This wouldn't work as it's not really a for loop but inside an ODE function which does the for loop. The only thing I could do it to create multiple ODE_funcs. But as there are many options, resulting in even more permutation of these options it would be crazy and give me a ton of files to keep up to date, so this does not work.

Iniciar sesión para comentar.

Amit
Amit el 20 de En. de 2014
This kind of optimization would really depend on your code. There is no generalization for this (to my knowledge).
If you post the code, or the segment of the code you're interested in, then one can give you suggestions to improve the performance.

3 comentarios

hey Amit,
Here's what the code looks like. It's a function I use together with ODE45:
function accelerations = ODE_func(x,t)
% Many calculations i always need to get certain forces
<CODE>
% Optional forces which i only need if the user toggle certain physical phenomenon on or off
if option1 == 1
% Do calcs
<CODE>
end
% Same
if option2 == 1
% Do calcs
<CODE>
end
end
If I now run a simulation with option1 == option2 == 0, then the two if-statements will always return false as the options don't change during the calculation. However as it's a time domain simulation ODE_func gets called many many times, meaning matlab has to check the if-statements every call to the function. I'm wondering if I can do these checks only once and then disable the if-statements for the entire calculation, thus saving me the overhead of returning the false's every timestep.
if option1 and option 2 does not turn to 1 simultaneously, then you can reduce the two if statements to if.. elseif.
However I'd think that might have done that already if that was the case.
Also, it surprises me that if statement would such a overhead in the calculations. By any chance, your ODE function is stiff? 'cause ode45 is very slow in those case and you can try ode15s which will be much faster and order of accuracy is still low to medium.
I indeed cannot group them as they independent. I'm also surprised they create so much overhead. Each if-statement now takes up 2-4% of the total calculation time and I have several so it adds up to double digits.
I will try again with a stiff solver, see if that increases the speed, thanks.
However it seems it is not possible to disable code blocks after a single if-statement check :(

Iniciar sesión para comentar.

Sounds like persistent variables may one of the solutions here. Persistent means that the variable will still remember its value the next time the function is called.
persistent condition;
if(isempty(condition)) % return true if condition is not initialized yet
%do calculations for check
if(something==true)
condition=true;
else
condition=false;
end
end
if(condition==true)
%do something
else
%do something else
end

3 comentarios

I'm now using global variables to import all settings and parameters into the ODE_func, which already gives significant overhead but that's another problem, so I don't think a persistent value would fix the problem.
hi I need to use persistent in my code but I am getting an error : persistent declaration is only allowed in a function ..
but I didn't need a function do you know how to do that ?
Matlab records the variables entered in the code file and stores it until the variables are explicitly cleared or deleted during the run, or if the code file contains any external function. when the solver switches between main file and functions, only the variables related to the current file are accessible.
Persistent variables store the varaibles in a function and retains it when the program calls the same function again avoiding the declaration of the variable each time. this is helpful when the function needs to be called multiple times, as persistent variables add higher time overhead. By using nested functions you can avoid the persistent variables.
Global variables are accessible to any function that are or will be called by the main program, time overhead is really high and this type of variable declaration causes buggy code and not a good programing method.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 20 de En. de 2014

Comentada:

el 9 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by