"GLOBAL can be very inefficient."

I use (in Matlab) the global statement inside the if command, so I only import the global variable into the local namespace if it is really necessary.

The code analyzer warns me that " global can be very inefficient, unless it is a top-level expression in its function." Thinking of a possible internal implementation, I find this limitation very strange and unusual. I think of two possibilities:

  • This really means that " global very inefficient, so don't use it in a loop." In particular, using it inside an if, like me, is completely safe, and the warning is issued incorrectly (and poorly worded)

  • The warning is true; Matlab uses some really unusual mechanism for loading variables in the background, so it’s actually much slower to import global variables inside an if statement. In this case, I would like to receive a hint or a pointer to how this material really works, because I am interested, and it seems important if I want to write effective code in the future.

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

Thanks in advance.

EDIT: make it clearer: I know that global slow (and apparently I can’t avoid using it, since this is the design solution of the old library that I use); I ask 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 

It’s hard for me to imagine the reason why the first should be slower than the second.

+4
source share
4 answers

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

[...] This is not necessarily more if not done in a top-level team, but people tend to put the construct in a loop or in several non-exclusive places in conditional structures. Having written mlint warnings, it’s much easier for a person not to add explanations, for example: “If you can’t prove that the global one will be executed only once, in which case it is no less effective, but it’s still bad”,

supports my option (1).

+3
source

In addition to the eykanals post, this tech note gives an explanation of why global is slow.

... when a function call includes global variables, performance is even more forbidden. This is due to the fact that in order to search for global variables, MATLAB must expand its search space outside the current workspace. In addition, the reason that calling a function with global variables looks much slower than others is because MATLAB Accelerator does not optimize such a function call.

+5
source

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

Be that as it may, I recommend reading the following two entries in the Mathworks blogs Lauren and Doug:

In short, global variables almost never go; There are many other ways to exchange variables — some of which she discusses — that are more efficient and less error prone.

+3
source

Fact (from Matlab 2014 to Matlab 2016a rather than using the Parallell toolkit): Often the fastest code you can achieve with Matlab is to do nested functions, separating your variables between functions without passing them.

A step close to this uses global variables and splits your project into multiple files. This might slow performance a bit, because (presumably, although I have never seen this checked in any tests). Matlab takes overhead from a global workspace, and because there is some kind of problem (presumably, although no evidence of this has ever been seen) with JIT acceleration.

Thanks to my own testing, the transfer of very large data matrices (hi-images) between function calls, the use of nested functions or global variables is almost the same in performance.

The reason you can get superior performance with global variables or nested functions is because you can avoid additional data copying this way. If you send a variable to a function, Matlab does this by reference, but if you change a variable in a function, Matlab makes a copy on the fly (copy on write). I do not know how to avoid this in Matlab, with the exception of nested functions and global variables. Any small leak that you get from obstacles to JIT or global sampling time is completely achieved, avoiding this extra data copy (when using big data).

It may have changed with nothing from the versions of Matlab, but from what I hear from friends, I doubt it. I can not imagine any test, I no longer have a Matlab license.

As proof, look no further than the video processing toolbar that I made that day when I was working with Matlab. This is terribly ugly under the hood because I was not able to get performance without globals.

This fact about Matlab (that global variables are the most optimized way that you can code when you need to modify big data in different functions) is a sign of the need to update the language and / or interpreter.

Instead, Matlab can use a better, more dynamic view of the workspace. But none of what I saw says this. Especially when you see that the user community seems to ignore the facts and put forward oppions for no reason: for example, using globals in Matlab is slow.

This is not true.

However, you should not use global variables. If you are forced to process real-time video in pure Matlab, and you find that you have no other choice to use the global features to achieve performance, you should get a hint and change the language. Its time to switch to higher-performing languages ​​... and also, perhaps, write a random analysis about stack overflows, in the hope that Matlab can be improved by swaying opponents of its users.

0
source

All Articles