2

I hate using global variables, and everyone should. If a language has no way around using global variables it should be updated. Currently, I don't know any good alternative to using global variables in Matlab, when efficiency is the goal.

Sharing data between callbacks can be done in only 4 ways that I am aware of:

  1. nested functions
  2. getappdata (what guidata uses)
  3. handle-derived class objects
  4. global variables

nested functions forces the entire project to be in a single m-file, and handle-derived class objects (sent to callbacks), gives unreasonable overhead last I checked.

Comparing getappdata/guidata with global variables, in a given callback you can write(assuming uglyGlobal exists as a 1000x1000 mat):

global uglyGlobal;
prettyLocal = uglyGlobal;
prettyLocal(10:100,10:100) = 0;
uglyGlobal = prettyLocal;

or you can write (assuming uglyAppdata exists as a 1000x1000 mat):

prettyLocal = getappdata(0,'uglyAppdata');
prettyLocal(10:100,10:100) = 0;
setappdata(0,'x',prettyLocal);

The above snippets should work in the same way. It could be (but is not guaranteed) more efficient with just:

global uglyGlobal;
uglyGlobal(10:100,10:100) = 0;

This snippet, unlike the previous ones, may not trigger a copy-on-write in Matlab. The data in the global workspace is modified, and (potentially) only there.

however, if we do the innocent modification:

global uglyGlobal;
prettyLocal = uglyGlobal;
uglyGlobal(10:100,10:100) = 0;

Matlab will ensure that prettyLocal gets its own copy of the data. The third line above will show up as the culprit when you profile. To make that worse on my brain(globals tend to do that), any other workspace that exists that has a local reference to the global, will make a copy-on-write trigger for that variable, one for each.

However, iff one makes sure no local references exists:

Is it true that global variables, used carefully can yield the best performance programs in Matlab?

Note: I would provide som timing results, but unfortunately I no longer have access to Matlab.

Community
  • 1
  • 1
Stefan Karlsson
  • 1,092
  • 9
  • 21
  • The problem with global variables, unless explicitly designed to be so (e.g. in julia) is not one of performance, but of design. When part of good design global variables may not only be ok, but even necessary and desirable. This is especially the case in matlab as a scientific language, where you might define lots of globals as the scientific variables that are constant throughout your problem. – Tasos Papastylianou Jan 28 '17 at 13:25
  • @TasosPapastylianou, in Matlab there is nothing deliberately done with globals in order for them to be mechanisms for performance tweaks. If the answer to my question is yes, it should be very significant performance tweaks a program can have, at the cost of forcing really non-intuitive and costly design patterns on programs. This should be a hint that the same thing should be able to be done without using globals, and so that mathworks should provide an upgrade... assuming the answer is yes, which frankly I hope it is not. – Stefan Karlsson Jan 28 '17 at 13:59
  • 1
    Have you see this: http://www.mathworks.com/matlabcentral/answers/99537-which-type-of-function-call-provides-better-performance-in-matlab ? – EBH Jan 28 '17 at 18:41
  • @EBH I keep forgetting about Matlab's new fancy JIT compilations :p Good article. Yeah it would make sense that globals would break optimisations, since matlab wouldn't necessarily know how to optimise them safely. – Tasos Papastylianou Jan 28 '17 at 21:40
  • @EBH, yes I read that when it was first posted. It is not quite the same thing as the case i describe in my question. – Stefan Karlsson Jan 29 '17 at 00:31

0 Answers0