Are Global Variables Transferred Significantly Slower Than Function Inputs?

10 次查看(过去 30 天)
Hello,
I have a few massive matrices which are used in a function that is called many thousands of times. Due to the way the function is accessed, these massive matrices are brought into the function as globals rather than inputs. If I run the computations within the function thousands of times (having loaded in the matrices once prior to the computations), it's extremely quick. However, when the full code is run, in which the matrices are brought in as globals every call, it's extremely slow.
I've come to the conclusion that it is indeed the global declaration that is slowing things down.
My question(s):
Would passing these massive matrices in as function inputs rather than globals be significantly faster (it's complicated enough to alter that I'd rather ask first)? Or, is there a way to reduce the time it takes for the global declaration to "run"?
I apologize if my question is unclear, I'll gladly provide additional information if needed.

采纳的回答

Walter Roberson
Walter Roberson 2017-1-22
Yes, see http://www.mathworks.com/matlabcentral/answers/99537-which-type-of-function-call-provides-better-performance-in-matlab (though I am not sure it is still the case that optimization is turned off for functions that use globals.)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by