Are Global Variables Transferred Significantly Slower Than Function Inputs?

11 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Jan 2017
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.)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!