Why does declaring a global variable take so much time?
古いコメントを表示
I have a function in from a larger program that I profiled in order to try and find some places to gain some speed. The global declaration in the function below takes up by far the most time. Can anyone tell me why this would take so much time and suggest possible solutions? Unfortunately, giving it as an argument to the function is not an option.

1 件のコメント
Stephen23
2020 年 8 月 13 日
"As far as I was aware ode45 needs an ode function that has just two arguments."
"However, looking a bit deeper, there is a way to pass more arguments."
採用された回答
その他の回答 (1 件)
Walter Roberson
2014 年 2 月 10 日
Try mechanisms such as
setter: setappdata(0,'P', P);
your function: P = getappdata(0, 'P');
4 件のコメント
José-Luis
2014 年 2 月 10 日
Isn't that like putting makeup on a global to make it look pretty? :) Also it would involve modifying the parent function, which Alexander apparently can't. Or am I missing something?
Walter Roberson
2014 年 2 月 10 日
It isn't just putting makeup on if it runs faster. I have not timed it so I do not know what the relative speeds are.
José-Luis
2014 年 2 月 10 日
That might well be, but it would have the disadvantages of a global, save that you need to explicitly call it. Might make debugging a pain. Oh well...
Walter Roberson
2014 年 2 月 10 日
One of the problems with global variables is that people accidentally reuse the name for local purposes, thus overwriting the global version. If the only way to overwrite the global version is to code a setappdata() call, then that is not going to happen accidentally.
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!