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.

 채택된 답변

per isakson
per isakson 2020년 5월 5일

0 개 추천

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

Thank you, how can I deal with it if I am loading M.mat in the following way from a class named utils
M = utils.get_M;
function M = get_M
M = load(fullfile(path, 'M.mat'));
end
Here, I couldn't do
M = utils.get_M;
function M = get_M
persistent M
if isempty(M)
M = load(fullfile(path,'M.mat'));
end
end
You provide too little context to know for sure. And you diverge from the original question.
utils.get_M is that intended to be a static method?
The code snippet
M = utils.get_M;
function M = get_M
M = load(fullfile(path, 'M.mat'));
end
cannot be part of a classdef file. It looks rather as being part of a script.
Deepa Maheshvare
Deepa Maheshvare 2020년 5월 5일
편집: per isakson 2020년 5월 5일
Sorry for being unclear.
Yes, utils.get_M is a static method
classdef io_utils
methods(Static)
function M = get_M
M = load(fullfile(path, 'M.mat'));
end
end
end
per isakson
per isakson 2020년 5월 5일
편집: per isakson 2020년 5월 5일
Your class, io_utils, will load M.mat for every call and will thus not improve the performance compared to fun2(t,x), rather the opposite. The class
classdef io_utils
properties ( Constant )
M = load('M.mat');
end
end
loads M.mat once. It's called
>> S = io_utils.M
S =
struct with fields:
A: [7×1001 double]
>>
Deepa Maheshvare
Deepa Maheshvare 2020년 5월 5일
편집: Deepa Maheshvare 2020년 5월 5일
Thanks . So I've
classdef utils
properties (Constant)
M = load(fullfile(path, 'M.mat'));
end
methods(Static)
function M = get_M
M = load(fullfile(path, 'M.mat'));
end
end
end
And I tried the following ways
Method 1:
function fun1()
[t,x] = ode15s(@(t,s) fun2(t,x), tspan , x0 ,options);
% other functions
end
function dx = fun2(t,x)
persistent M
if isempty(M)
M = utils.get_M;
end
end
:
:
dx = M*x
end
Method 2:
function fun1()
[t,x] = ode15s(@(t,s) fun2(t,x), tspan , x0 ,options);
end
function dx = fun2(t,x)
M = utils.M
end
:
:
dx = M*x
end
In the first case, M = utils.get_M is called once and in the second method M = utils.M is called as many times fun2 is called by fun1. From what you have explained, is it right to understand `class utils` is loaded only once even if it's called multiple times.
per isakson
per isakson 2020년 5월 5일
편집: per isakson 2020년 5월 5일
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 2020년 5월 6일
편집: per isakson 2020년 5월 7일
Cool! I've read and implemented it already. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 5월 5일

편집:

2020년 5월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by