Huge time difference if function is called from within another function

4 ビュー (過去 30 日間)
tirupam goel
tirupam goel 2015 年 8 月 5 日
コメント済み: tirupam goel 2015 年 8 月 7 日
Function A, when called directly, runs in 10 minutes. However, if function A is called by function B, and I run function B, the time spent of running function A is 100 minutes or so. Is this is a feature of Matlab?
Also, if I run two instances of Matlab, and run the same function on both these instances, can it happen that the first instance takes less time compared to the other?
  2 件のコメント
James Tursa
James Tursa 2015 年 8 月 5 日
You need to give more information about what functions you are calling and how you are calling them. There is no generic 10x time penalty for calling a function from within another function.
tirupam goel
tirupam goel 2015 年 8 月 7 日
Thank you. Given the complexity of my code, I thought it would be better to simplify the question. I figured the issue. Suppose function A needs another functions C. It matters if function C is passed as a parameter to function B (which then passes it to function A), versus function C being constructed within function B. The former is much faster, and it worked for me.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 5 日
What you describe could happen if function B restricts the number of threads to execute on, or opens a parpool that is smaller than the number of cores, and A is working with large matrices using data patterns that can normally be divided up into multiple cores automatically by BLAS or MLK or the like.
  1 件のコメント
tirupam goel
tirupam goel 2015 年 8 月 7 日
Thank you. Parallel code was not the issue. I figured the issue. Suppose function A needs another functions C. It matters if function C is passed as a parameter to function B (which then passes it to function A), versus function C being constructed within function B. The former is much faster, and it worked for me.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by