using feval or not?

30 ビュー (過去 30 日間)
Bruno Luong
Bruno Luong 2023 年 7 月 29 日
編集済み: Bruno Luong 2023 年 7 月 31 日
I have function handle myfun I need to evalute
y = feval(myfun, x1, x2)
instead of direct call
y = myfun(x1, x2)
because when reading code in the first syntax I know then myfun is a function handle, and in the second syntax there might be a confusion of what myfun is. So for readability and maintainability I prefer using feval.
Note that myfun is NOT a string or char.
I test the speed and it seems to me there is no speed penalty one way or another.
I even think the presence of feval in the code will help the parser-compiler to have specific code of evaluation this function at the compiation time and not at a decision at the runtime, so in theory feval could be faster.
Is there any argument againts using feval?
  9 件のコメント
Rik
Rik 2023 年 7 月 31 日

I was just saying I'm using feval instead of the direct call method. I don't have my installs handy, so I can't look it up for you when the direct call became possible.

At any rate, feval predates anonymous functions (which were introduced in v7). I can't check right now if v6 already had it, but v6.5 definitely does. (I don't extend the compatibility range of my functions to v6, since it doesn't support && and , which would require a lot of extra work in coding, for just a handful of people globally who would benefit)

Bruno Luong
Bruno Luong 2023 年 7 月 31 日
編集済み: Bruno Luong 2023 年 7 月 31 日
@Rik "so I can't look it up for you when the direct call became possible."
When function handle is introduced. never mind this is not relevant to my question

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

採用された回答

Jan
Jan 2023 年 7 月 30 日
I do not see problems with using feval.
There have been a speed penalty in old versions, as far as I remember R2009a. But now the JIT-acceleration seems to remove the overhead automatically.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by