How to improve this function execution?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I have a code that includes a complex function which returns a column vector.
The function can be explained in this simplified form:
V = @(S1, S2, S3, S4, X1, X2, X3, X4) [...
a * S1/(S1 + S2) * X1;
b * X2;
c * S2/(S3 + S4) * X3;
d * S1/(S1 + S4) * X4;
....
];
where S and X are variables; a,b,c,d constants.
Since the code provides many calls of this function, and the profiler graph has clearly shown the large time necessary for its execution, how can I do to improve the performances?
I'm not interested to modify the overall code, but only the management of this function. Maybe, could I enter the function in different format (binary, csv,...) or as a file? Currently, the function is inside one of the code-related scripts (.m file).
Thanks
3 件のコメント
回答 (1 件)
Matt J
2022 年 7 月 19 日
編集済み: Matt J
2022 年 7 月 19 日
Since the code provides many calls of this function
Instead of calling the function multiple times, call it just one time or just a few times but with vector inputs. To enable vectorized input, use .* and ./
[a,b,c,d]=deal(1);
V = @(S1, S2, S3, S4, X1, X2, X3, X4) [...
a .* S1./(S1 + S2) .* X1;
b .* X2;
c .* S2./(S3 + S4) .* X3;
d .* S1./(S1 + S4) .* X4
];
X1=rand(1,5);
X2=rand(1,5);
X3=rand(1,5);
X4=rand(1,5);
S1=rand(1,5);
S2=rand(1,5);
S3=rand(1,5);
S4=rand(1,5);
V(S1, S2, S3, S4, X1, X2, X3, X4)
2 件のコメント
Matt J
2022 年 7 月 21 日
編集済み: Matt J
2022 年 7 月 21 日
But I need to consider them as elements, not vector, because of other aspects of the code
That sounds doubtful. The better line of inquiry might be, do you really have to do that.
But anyway, the answer to your question is, no, you cannot speed up the execution if you must process the inputs as scalars, other than perhaps to use parallel computing toolbox functions like parfor.
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!