7

I am using (in Matlab) a global statement inside an if command, so that I import the global variable into the local namespace only if it is really needed.

The code analyzer warns me that "global could be very inefficient unless it is a top-level statement in its function". Thinking about possible internal implementation, I find this restriction very strange and unusual. I am thinking about two possibilities:

  1. What this warning really means is "global is very inefficient of its own, so don't use it in a loop". In particular, using it inside an if, like I'm doing, is perfectly safe, and the warning is issued wrongly (and poorly worded)

  2. The warning is correct; Matlab uses some really unusual variable loading mechanism in the background, so it is really much slower to import global variables inside an if statement. In this case, I'd like to have a hint or a pointer to how this stuff really works, because I am interested and it seems to be important if I want to write efficient code in future.

Which one of these two explanations is correct? (or maybe neither is?)

Thanks in advance.

EDIT: to make it clearer: I know that global is slow (and apparently I can't avoid using it, as it is a design decision of an old library I am using); what I am asking is why the Matlab code analyzer complains about

if(foo==bar)
    GLOBAL baz
    baz=1;
else
    do_other_stuff;
end

but not about

GLOBAL baz
if(foo==bar)
    baz=1;
else
    do_other_stuff;
end

I find it difficult to imagine a reason why the first should be slower than the second.

RTbecard
  • 868
  • 1
  • 8
  • 23
Federico Poloni
  • 668
  • 8
  • 24
  • This is cross-posted to http://mathworks.com/matlabcentral/answers/19316-global-could-be-very-inefficient#answer_25760. Sorry about that: I first wanted to post only there, but I got a 404, so I assumed it hadn't worked and switched to SO. – Federico Poloni Oct 26 '11 at 09:23

4 Answers4

6

To supplement eykanals post, this technical note gives an explanation to why global is slow.

... when a function call involves global variables, performance is even more inhibited. This is because to look for global variables, MATLAB has to expand its search space to the outside of the current workspace. Furthermore, the reason a function call involving global variables appears a lot slower than the others is that MATLAB Accelerator does not optimize such a function call.

Ghaul
  • 3,340
  • 1
  • 19
  • 24
  • Interesting (and it would be even more if it included function handles), but this does not answer my question. – Federico Poloni Oct 25 '11 at 13:59
  • @FedericoPoloni - What do you mean? This directly answers your question, and it's from MathWorks themselves. Globals are slow because (1) the engine has to search outside the workspace when finding the variable, and (2) the accelerator doesn't optimize global calls. – eykanal Oct 25 '11 at 14:42
  • Maybe I wasn't clear enough in the question, but I don't know how to reword it. What I mean to ask is: "why is the code analyzer complaining about a `global` in an `if` statement? Is it really true that `global` inside an `if` is slower than one in a "top-level statement", or is it just a poorly worded warning?" I know that `global` is slow, but this is something slightly different. – Federico Poloni Oct 25 '11 at 21:32
3

I do not know the answer, but I strongly suspect this has to do with how memory is allocated and shared at runtime.

Be that as it may, I recommend reading the following two entries on the Mathworks blogs by Loren and Doug:

  1. Writing deployable code, the very first thing he writes in that post
  2. Top 10 MATLAB code practices that make me cry, #2 on that list.

Long story short, global variables are almost never the way to go; there are many other ways to accomplish variable sharing - some of which she discusses - which are more efficient and less error-prone.

Clement J.
  • 3,012
  • 26
  • 29
eykanal
  • 26,437
  • 19
  • 82
  • 113
  • Thanks for sharing, but global variables are a choice of the library I am using (LYAPACK), so I cannot change it. – Federico Poloni Oct 25 '11 at 13:56
  • @Jonas - You know, I never bothered to even look at that picture on the sidebar of her blog. Thanks for pointing that out. – eykanal Oct 25 '11 at 13:57
3

The answer from Walter Roberson here http://mathworks.com/matlabcentral/answers/19316-global-could-be-very-inefficient#answer_25760

[...] This is not necessarily more work if not done in a top-level command, but people would tend to put the construct in a loop, or in multiple non-exclusive places in conditional structures. It is a lot easier for a person writing mlint warnings to not have to add clarifications like, "Unless you can prove those "global" will only be executed once, in which case it isn't less efficient but it is still bad form"

supports my option (1).

Federico Poloni
  • 668
  • 8
  • 24
0

Fact(from Matlab 2014 up until Matlab 2016a, and not using parallell toolbox): often, the fastest code you can achieve with Matlab is by doing nested functions, sharing your variables between functions without passing them.

The step close to that, is using global variables, and splitting your project up into multiple files. This may pull down performance slightly, because (supposedly, although I have never seen it verified in any tests) Matlab incurs overhead by retrieving from the global workspace, and because there is some kind of problem (supposedly, although never seen any evidence of it) with the JIT acceleration.

Through my own testing, passing very large data matrices (hi-res images) between calls to functions, using nested functions or global variables are almost identical in performance.

The reason that you can get superior performance with global variables or nested functions, is because you can avoid having extra data copying that way. If you send a variable to function, Matlab does so by reference, but if you modify the variable in the function, Matlab makes a copy on the fly (copy-on-write). There is no way I know of to avoid that in Matlab, except by nested functions and global variables. Any small drain you get from hinderance to JIT or global fetch times, is totally gained by avoiding this extra data copying, (when using larger data).

This may have changed with never versions of Matlab, but from what i hear from friends, I doubt it. I cant submit any test, dont have a Matlab license anymore.

As proof, look no further then this toolbox of video processing i made back in the day I was working with Matlab. It is horribly ugly under the hood, because I had no way of getting performance without globals.

This fact about Matlab (that global variables is the most optimized way you can code when you need to modify large data in different functions), is an indication that the language and/or interpreter needs to be updated.

Instead, Matlab could use a better, more dynamic notion of workspace. But nothing I have seen indicates this will ever happen. Especially when you see the community of users seemingly ignore the facts, and push forward oppions without any basis: such as using globals in Matlab are slow.

They are not.

That said, you shouldnt use globals, ever. If you are forced to do real time video processing in pure Matlab, and you find you have no other option then using globals to reach performance, you should get the hint and change language. Its time to get into higher performance languages.... and also maybe write an occasional rant on stack overflow, in hopes that Matlab can get improved by swaying the oppinions of its users.

Stefan Karlsson
  • 1,092
  • 9
  • 21