Optimizing code run time

2 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 5 月 5 日
編集済み: per isakson 2020 年 5 月 7 日
Hi All,
I've the following function
function fun1()
[t,x] = ode15s(@(t,s) fun2(t,x), tspan , x0 ,options);
% other functions
function dx = fun2(t,x)
M = load(fullfile(path,'M.mat'));
dx = M*x
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.


per isakson
per isakson 2020 年 5 月 5 日
Try to replace
M = load(fullfile(path,'M.mat'));
persistent M
if isempty(M)
M = load(fullfile(path,'M.mat'));
and see persistent
  7 件のコメント
Deepa Maheshvare
Deepa Maheshvare 2020 年 5 月 6 日
Cool! I've read and implemented it already. Thanks.


その他の回答 (0 件)




Community Treasure Hunt

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

Start Hunting!

Translated by