Function handles and coder

4 ビュー (過去 30 日間)
Maria
Maria 2020 年 11 月 9 日
コメント済み: Maria 2021 年 3 月 1 日
Hi,
I have a very generic question.
I have a matlab function "myfunction", and I would normally pass to that function a function handle, something like
fun = @(x) (x-2);
result = myfunction(fun, a, b);
So far so good.
Now, I need my code to run very fast. So, I was suggested to remove the function handle, so that I define my "fun" inside "myfunction", as this would speed up the code. I tried, and it indeed got faster.
Then, I should do a mex of "myfunction" using Matlab coder.
My problem is that I have a loooot of "fun" and it is horrible to write a "myfunction" for each of them. However, I would do it for a faster code. But, I am now wondering if it is useless all this "manual" work because the coder is probably smart enough to "inline" my "fun" inside each call of "myfunction".
So: with the goal of a "fast" code, does it make sense for me to write a personalized "myfunction" for each "fun", or it is completely useless because the Matlab coder would in any case do it for me?

回答 (1 件)

Puru Kathuria
Puru Kathuria 2021 年 2 月 26 日
Hi,
If the functions are mostly similar then you can differentiate them by writing a wrapper on top of them and seperating their functionality by passing an arguement to the wrapper. Further, generate code for the wrapper file.
Otherwise, you might also make each of them code generatable for faster performance, depending upon your use cases.
  1 件のコメント
Maria
Maria 2021 年 3 月 1 日
Hi Puru,
Thank you for your answer. I am not sure if I fully understand. Could I ask you if you may have an example, on how to write a wrapper?
About the second option you suggest, do you mean that each "fun" should be mexed, or what do you exactly mean by "making them code generatable"?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by