フィルターのクリア

speed, functions and overheads

24 ビュー (過去 30 日間)
Patrick Mboma
Patrick Mboma 2013 年 6 月 24 日
コメント済み: TallBrian 2019 年 10 月 15 日
Dear all,
I have noticed using functions, nested functions and sub-functions considerably decrease the speed of execution. Is it possible to decrease the overhead incurred when calling a function?
Thanks, Patrick
  2 件のコメント
Matt J
Matt J 2013 年 6 月 24 日
Decrease it from what? What baseline are you comparing it to?
Sean de Wolski
Sean de Wolski 2013 年 6 月 24 日
What are you doing?
Have you used the profiler to identify bottlenecks in your code that are more than likely killing way more time than function overhead?

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

採用された回答

Jan
Jan 2013 年 6 月 24 日
編集済み: Jan 2013 年 6 月 25 日
x = rand(1000, 1000);
tic;
for k = 1:1e3
m = mean(x);
end
toc;
tic;
for k = 1:1e3
m = sum(x, 1) / size(x, 1);
end
toc;
I assume, but cannot test currently, that there is a measurable difference in the timing due to the overhead for calling the M-function mean() (at least until 2009a it was an M-file).
Of course there is no magic flag to reduce the overhead, otherwise TMW would not have disabled this. So inlining the code is the only way to avoid the overhead. The resulting code is faster for the price of a reduced readability.
[EDITED] Summary of my comments:
  • When you call functions, you cannot avoid the overhead.
  • When you inline the code instead, the overhead vanishes, but other difficulties arise.
  • Tertium non datur.
  7 件のコメント
Jan
Jan 2013 年 6 月 25 日
編集済み: Jan 2013 年 6 月 25 日
@Patrick: Let me repeat again, that the example is not "too simple" but chosen as simple as possible on purpose to demonstrate the drawbacks and benefits of inlining versus calling functions.
Of course I know, that calling mean() will not solve your problem. But you ask for avoiding the overhead of calling a function. And the answer is simply: See [EDITED].
TallBrian
TallBrian 2019 年 10 月 15 日
@Jan, this answer was from back in 2013, do you know is the answer still the same with the latest MATLAB?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by