0

The situation: I'm coding a heavy simulation model, that runs on a Matrix which could be quite big (say 1025*1025 cells). In every swipe (numel(myMatrix) iterations) of the model the matrix is passed between functions, and most of the time it is changed (so MATLAB does not pass it by reference). Because there is a huge number (several millions) of iterations I use Mex code for the core of the model, and only pass the simulation parameters to it.

My question: In order to save some time I used global variable for the main matrix, but I read all over the web that this is a big 'NO! NO!'. So I tried to use nested functions instead, but then I found that "Nested functions are currently unsupported for code generation". So, what should I do? Is there a smart way to avoid globals (which I tend to dislike in my code) and some how implement the idea of nested function in a MEX file?

Any help will be appreciated!

EBH
  • 10,350
  • 3
  • 34
  • 59
  • If it isn't broken, why are you trying to change it? – sco1 Jul 05 '16 at 19:00
  • This is more general question since I'm about to write another model like this soon, and would like to adopt healthy habits :) – EBH Jul 05 '16 at 19:05
  • 3
    The short answer is try it and see for yourself. Without knowing specifics it's impossible to answer. General disadvantages of globals aside, MATLAB's gobals in many cases (at least historically) are actually [the slowest method](http://www.mathworks.com/matlabcentral/answers/99537-which-type-of-function-call-provides-better-performance-in-matlab#answer_108884). Whether or not this applies to your situation and if it's actually a measurable difference is highly dependent on your code and the version of MATLAB being used. – sco1 Jul 05 '16 at 19:15
  • @excaza thanks for that link, very usefull! – EBH Jul 05 '16 at 19:37

0 Answers0