Optimizing code run time

2 visualizaciones (últimos 30 días)
Deepa Maheshvare
Deepa Maheshvare el 5 de Mayo de 2020
Editada: per isakson el 7 de Mayo de 2020
Hi All,
I've the following function
function fun1()
[t,x] = ode15s(@(t,s) fun2(t,x), tspan , x0 ,options);
% other functions
end
function dx = fun2(t,x)
M = load(fullfile(path,'M.mat'));
:
:
dx = M*x
end
Each time fun2 is called, the same file is loaded and this increases the compute time
(calls: 52085 total time:156.848s, self time:59.780s). Of the total time taken (209s), 156s is taken for executing fun1 and 95% of the time is spent in loading the input file.
I'd like to ask for suggestions on how to get around this i.e reduce the time taken for loading/avoid loading for each function call.

Respuesta aceptada

per isakson
per isakson el 5 de Mayo de 2020
Try to replace
M = load(fullfile(path,'M.mat'));
by
persistent M
if isempty(M)
M = load(fullfile(path,'M.mat'));
end
and see persistent
  7 comentarios
per isakson
per isakson el 5 de Mayo de 2020
Editada: per isakson el 5 de Mayo de 2020
MATLAB evaluates the expressions when loading the class. Therefore, the values MATLAB assigns to RN are the result of a single call to the rand function and do not change with subsequent references to NamedConst.RN.
Searching and reading the documentation is a large part of mastering Matlab.
Method 1.
I see no point in using the static method, get_H. It save on overhead to call load() directly in fun2.
Method 2.
Yes. (However, it's more convicing to test than listen to me.)
Deepa Maheshvare
Deepa Maheshvare el 6 de Mayo de 2020
Editada: per isakson el 7 de Mayo de 2020
Cool! I've read and implemented it already. Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by